/** * Retrieves rate limit configuration for the supplied key */ public RequestRateInfo getRequestRateLimit(String key) { RequestRateInfo ri = this.state.requestRateLimits.get(key); if (ri == null) { return null; } return Utils.clone(ri); }
public Query getResourceQuery(Action action) { if (this.resourceQueryMap == null) { return null; } return Utils.clone(this.resourceQueryMap.get(action)); }
public SystemHostInfo getSystemInfo() { if (!this.info.properties.isEmpty() && !this.info.ipAddresses.isEmpty()) { return Utils.clone(this.info); } return updateSystemInfo(true); }
public ServiceHostState getState() { ServiceHostState s = Utils.clone(this.state); s.systemInfo = getSystemInfo(); return s; }
public Operation setBody(Object body) { if (body != null) { if (hasOption(OperationOption.CLONING_DISABLED)) { this.body = body; } else { this.body = Utils.clone(body); } } else { this.body = null; } return this; }
private ReliableSubscriptionService(Operation subscribeOp, ServiceSubscriber sr, Consumer<Operation> notificationConsumer) { // cache the subscribe operation since we will clone and re-use if we need to // re-subscribe. It contains the appropriate authorization context this.subscribeOp = subscribeOp.clone(); // cache the request details this.subscribeRequest = Utils.clone(sr); this.consumer = notificationConsumer; }
/** * See {@link #setRequestRateLimit(String, double)} */ public ServiceHost setRequestRateLimit(String key, RequestRateInfo ri) { if (ri.limit <= 0.0) { throw new IllegalArgumentException("limit must be a non zero positive number"); } ri = Utils.clone(ri); if (ri.timeSeries == null) { ri.timeSeries = new TimeSeriesStats( 60, TimeUnit.SECONDS.toMillis(1), EnumSet.of(AggregationType.SUM)); } else if (!ri.timeSeries.aggregationType.contains(AggregationType.SUM)) { throw new IllegalArgumentException( "time series must be of type " + AggregationType.SUM); } if (ri.options == null || ri.options.isEmpty()) { ri.options = EnumSet.of(Option.FAIL); } // overwrite any existing limit this.state.requestRateLimits.put(key, ri); return this; }
private Operation createCloneOperation(String resourceType, ServiceDocument component, String projectLink) { if (component instanceof ResourceState) { String factoryLink = CompositeComponentRegistry .descriptionFactoryLinkByDescriptionLink(component.documentSelfLink); if (factoryLink == null) { throw new LocalizableValidationException( "Cannot clone unsupported type " + resourceType, "compute.clone.unsupported.type", resourceType); } ResourceState cloned = Utils.clone((ResourceState) component); if (cloned.customProperties == null) { cloned.customProperties = new HashMap<>(); } cloned.customProperties.put(CloneableResource.PARENT_RESOURCE_LINK_PROPERTY_NAME, cloned.documentSelfLink); cloned.documentSelfLink = null; Operation post = Operation.createPost(this, factoryLink) .setBody(cloned); if (projectLink != null && projectLink.isEmpty()) { post.addRequestHeader(OperationUtil.PROJECT_ADMIRAL_HEADER, projectLink); } return post; } throw new LocalizableValidationException("Cannot clone unsupported type " + resourceType, "compute.clone.unsupported.type", resourceType); }
body = Utils.clone(body); body.associatedOp = op;
/** * Invoke the service setInitialState method and ensures the state has proper self link and * kind. If the service is not marked with {@link ServiceOption#IMMUTABLE}, the state * is serialized to JSON to verify serialization is possible, and cloned */ void normalizeInitialServiceState(Service s, Operation post, Long finalVersion) { if (!post.hasBody()) { return; } // We force serialize to JSON to clone // and prove the state *is* convertible to JSON. It also forces type // to the service state type through type coercion Object body = post.getBodyRaw(); if (!body.getClass().equals(s.getStateType())) { body = Utils.toJson(body); } ServiceDocument initialState = s.setInitialState( body, finalVersion); initialState.documentSelfLink = s.getSelfLink(); initialState.documentKind = Utils.buildKind(initialState.getClass()); initialState.documentAuthPrincipalLink = (post.getAuthorizationContext() != null) ? post .getAuthorizationContext().getClaims().getSubject() : null; if (!isServiceImmutable(s)) { initialState = Utils.clone(initialState); } post.setBodyNoCloning(initialState); }
@Override public void handleStart(Operation startPost) { NodeGroupState initState = null; if (startPost.hasBody()) { initState = startPost.getBody(NodeGroupState.class); } else { initState = new NodeGroupState(); } initState.documentOwner = getHost().getId(); if (initState.config == null) { initState.config = new NodeGroupConfig(); } NodeState self = initState.nodes.get(getHost().getId()); self = buildLocalNodeState(self); if (!validateNodeOptions(startPost, self.options)) { return; } initState.nodes.put(self.id, self); this.cachedState = Utils.clone(initState); startPost.setBody(this.cachedState).complete(); }
@Test public void testSerializeClassesWithoutDefaultConstructor() { Range range = new Range(0, 100); // clone uses kryo serialization Range clone = Utils.clone(range); assertEquals(range.from, clone.from); assertEquals(range.to, clone.to); }
@Test public void testSerializeClassesWithoutDefaultConstructor() { Range range = new Range(0, 100); // clone uses kryo serialization Range clone = Utils.clone(range); assertEquals(range.from, clone.from); assertEquals(range.to, clone.to); }
private void doExampleServicePatch(Map<String, ExampleServiceState> states, URI hostUri) throws Throwable { this.host.log("Starting PATCH to %d example services", states.size()); TestContext ctx = this.host .testCreate(this.updateCount * states.size()); this.setOperationTimeoutMicros(TimeUnit.SECONDS.toMicros(this.host.getTimeoutSeconds())); for (int i = 0; i < this.updateCount; i++) { for (Entry<String, ExampleServiceState> e : states.entrySet()) { ExampleServiceState st = Utils.clone(e.getValue()); st.counter = (long) i; Operation patch = Operation .createPatch(UriUtils.buildUri(hostUri, e.getKey())) .setCompletion(ctx.getCompletion()) .setBody(st); this.host.send(patch); } } this.host.testWait(ctx); this.host.log("Done with PATCH to %d example services", states.size()); }
private void doExampleServicePatch(Map<String, ExampleServiceState> states, URI hostUri) throws Throwable { this.host.log("Starting PATCH to %d example services", states.size()); TestContext ctx = this.host .testCreate(this.updateCount * states.size()); this.setOperationTimeoutMicros(TimeUnit.SECONDS.toMicros(this.host.getTimeoutSeconds())); for (int i = 0; i < this.updateCount; i++) { for (Entry<String, ExampleServiceState> e : states.entrySet()) { ExampleServiceState st = Utils.clone(e.getValue()); st.counter = (long) i; Operation patch = Operation .createPatch(UriUtils.buildUri(hostUri, e.getKey())) .setCompletion(ctx.getCompletion()) .setBody(st); this.host.send(patch); } } this.host.testWait(ctx); this.host.log("Done with PATCH to %d example services", states.size()); }
private void queryComputesToUnassign(EpzComputeEnumerationTaskState state) { Query mustBeOut = Utils.clone(state.resourcePoolQuery) .setOccurance(Occurance.MUST_NOT_OCCUR); Query areIn = Query.Builder.create() .addKindFieldClause(ComputeState.class) .addCompositeFieldClause(ResourceState.FIELD_NAME_CUSTOM_PROPERTIES, EPZ_CUSTOM_PROP_NAME_PREFIX + extractRpId(state), EPZ_CUSTOM_PROP_VALUE) .build(); Query combinedQuery = Query.Builder.create().addClauses(mustBeOut, areIn).build(); queryComputes(state, combinedQuery, EpzComputeEnumerationTaskState.SubStage.UNASSIGN_COMPUTES, EpzComputeEnumerationTaskState.SubStage.QUERY_COMPUTES_TO_ASSIGN); }
private void queryComputesToAssign(EpzComputeEnumerationTaskState state) { Query mustBeIn = Utils.clone(state.resourcePoolQuery); Query areOut = Query.Builder.create() .addKindFieldClause(ComputeState.class) .addFieldClause( QuerySpecification.buildCompositeFieldName( ResourceState.FIELD_NAME_CUSTOM_PROPERTIES, EPZ_CUSTOM_PROP_NAME_PREFIX + extractRpId(state)), EPZ_CUSTOM_PROP_VALUE, MatchType.TERM, Occurance.MUST_NOT_OCCUR) .build(); Query combinedQuery = Query.Builder.create().addClauses(mustBeIn, areOut).build(); queryComputes(state, combinedQuery, EpzComputeEnumerationTaskState.SubStage.ASSIGN_COMPUTES, EpzComputeEnumerationTaskState.SubStage.COMPLETED); }
private void subscribeToService(URI uri, ServiceSubscriber sr) { if (sr.usePublicUri) { sr = Utils.clone(sr); sr.reference = UriUtils.buildPublicUri(this.host, sr.reference.getPath()); } URI subUri = UriUtils.buildSubscriptionUri(uri); this.host.send(Operation.createPost(subUri) .setCompletion(this.host.getCompletion()) .setReferer(this.host.getReferer()) .setBody(sr) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_QUEUE_FOR_SERVICE_AVAILABILITY)); }
private void subscribeToService(URI uri, ServiceSubscriber sr) { if (sr.usePublicUri) { sr = Utils.clone(sr); sr.reference = UriUtils.buildPublicUri(this.host, sr.reference.getPath()); } URI subUri = UriUtils.buildSubscriptionUri(uri); this.host.send(Operation.createPost(subUri) .setCompletion(this.host.getCompletion()) .setReferer(this.host.getReferer()) .setBody(sr) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_QUEUE_FOR_SERVICE_AVAILABILITY)); }
@Test public void ensureSerializationAndCloning() throws Throwable { TestTaskServiceDocument task = new TestTaskServiceDocument(); task.taskSubStage = DefaultSubStage.PROCESSING; task.taskInfo = new TaskState(); task.taskInfo.stage = TaskStage.CREATED; task = Utils.clone(task); assertEquals(DefaultSubStage.PROCESSING, task.taskSubStage); assertEquals(TaskStage.CREATED, task.taskInfo.stage); String jsonTask = Utils.toJson(task); task = Utils.fromJson(jsonTask, TestTaskServiceDocument.class); assertEquals(DefaultSubStage.PROCESSING, task.taskSubStage); assertEquals(TaskStage.CREATED, task.taskInfo.stage); }