List of usage examples for java.util.concurrent CompletableFuture completedFuture
public static <U> CompletableFuture<U> completedFuture(U value)
From source file:com.yahoo.pulsar.broker.service.ServerCnx.java
@Override protected void handleProducer(final CommandProducer cmdProducer) { checkArgument(state == State.Connected); CompletableFuture<Boolean> authorizationFuture; if (service.isAuthorizationEnabled()) { authorizationFuture = service.getAuthorizationManager() .canProduceAsync(DestinationName.get(cmdProducer.getTopic().toString()), authRole); } else {//from ww w . j a va 2s . c om authorizationFuture = CompletableFuture.completedFuture(true); } // Use producer name provided by client if present final String producerName = cmdProducer.hasProducerName() ? cmdProducer.getProducerName() : service.generateUniqueProducerName(); final String topicName = cmdProducer.getTopic(); final long producerId = cmdProducer.getProducerId(); final long requestId = cmdProducer.getRequestId(); authorizationFuture.thenApply(isAuthorized -> { if (isAuthorized) { if (log.isDebugEnabled()) { log.debug("[{}] Client is authorized to Produce with role {}", remoteAddress, authRole); } CompletableFuture<Producer> producerFuture = new CompletableFuture<>(); CompletableFuture<Producer> existingProducerFuture = producers.putIfAbsent(producerId, producerFuture); if (existingProducerFuture != null) { if (existingProducerFuture.isDone() && !existingProducerFuture.isCompletedExceptionally()) { Producer producer = existingProducerFuture.getNow(null); log.info("[{}] Producer with the same id is already created: {}", remoteAddress, producer); ctx.writeAndFlush(Commands.newProducerSuccess(requestId, producer.getProducerName())); return null; } else { // There was an early request to create a producer with // same producerId. This can happen when // client // timeout is lower the broker timeouts. We need to wait // until the previous producer creation // request // either complete or fails. ServerError error = !existingProducerFuture.isDone() ? ServerError.ServiceNotReady : getErrorCode(existingProducerFuture); log.warn("[{}][{}] Producer is already present on the connection", remoteAddress, topicName); ctx.writeAndFlush(Commands.newError(requestId, error, "Producer is already present on the connection")); return null; } } log.info("[{}][{}] Creating producer. producerId={}", remoteAddress, topicName, producerId); service.getTopic(topicName).thenAccept((Topic topic) -> { // Before creating producer, check if backlog quota exceeded // on topic if (topic.isBacklogQuotaExceeded(producerName)) { IllegalStateException illegalStateException = new IllegalStateException( "Cannot create producer on topic with backlog quota exceeded"); BacklogQuota.RetentionPolicy retentionPolicy = topic.getBacklogQuota().getPolicy(); if (retentionPolicy == BacklogQuota.RetentionPolicy.producer_request_hold) { ctx.writeAndFlush( Commands.newError(requestId, ServerError.ProducerBlockedQuotaExceededError, illegalStateException.getMessage())); } else if (retentionPolicy == BacklogQuota.RetentionPolicy.producer_exception) { ctx.writeAndFlush( Commands.newError(requestId, ServerError.ProducerBlockedQuotaExceededException, illegalStateException.getMessage())); } producerFuture.completeExceptionally(illegalStateException); producers.remove(producerId, producerFuture); return; } disableTcpNoDelayIfNeeded(topicName, producerName); Producer producer = new Producer(topic, ServerCnx.this, producerId, producerName, authRole); try { topic.addProducer(producer); if (isActive()) { if (producerFuture.complete(producer)) { log.info("[{}] Created new producer: {}", remoteAddress, producer); ctx.writeAndFlush(Commands.newProducerSuccess(requestId, producerName)); return; } else { // The producer's future was completed before by // a close command producer.closeNow(); log.info("[{}] Cleared producer created after timeout on client side {}", remoteAddress, producer); } } else { producer.closeNow(); log.info("[{}] Cleared producer created after connection was closed: {}", remoteAddress, producer); producerFuture.completeExceptionally( new IllegalStateException("Producer created after connection was closed")); } } catch (BrokerServiceException ise) { log.error("[{}] Failed to add producer to topic {}: {}", remoteAddress, topicName, ise.getMessage()); ctx.writeAndFlush(Commands.newError(requestId, BrokerServiceException.getClientErrorCode(ise), ise.getMessage())); producerFuture.completeExceptionally(ise); } producers.remove(producerId, producerFuture); }).exceptionally(exception -> { Throwable cause = exception.getCause(); if (!(cause instanceof ServiceUnitNotReadyException)) { // Do not print stack traces for expected exceptions log.error("[{}] Failed to create topic {}", remoteAddress, topicName, exception); } // If client timed out, the future would have been completed // by subsequent close. Send error back to // client, only if not completed already. if (producerFuture.completeExceptionally(exception)) { ctx.writeAndFlush(Commands.newError(requestId, BrokerServiceException.getClientErrorCode(cause), cause.getMessage())); } producers.remove(producerId, producerFuture); return null; }); } else { String msg = "Client is not authorized to Produce"; log.warn("[{}] {} with role {}", remoteAddress, msg, authRole); ctx.writeAndFlush(Commands.newError(requestId, ServerError.AuthorizationError, msg)); } return null; }); }
From source file:com.ikanow.aleph2.management_db.mongodb.services.TestIkanowV1SyncService_LibraryJars.java
@Test public void test_updateV1SourceStatus() throws JsonProcessingException, IOException, InterruptedException, ExecutionException, ParseException { @SuppressWarnings("unchecked") ICrudService<JsonNode> v1_share_db = this._service_context.getCoreManagementDbService() .getUnderlyingPlatformDriver(ICrudService.class, Optional.of("social.share")).get(); final DBCollection dbc = v1_share_db.getUnderlyingPlatformDriver(DBCollection.class, Optional.empty()) .get();/*from ww w. j a va 2s. co m*/ v1_share_db.deleteDatastore().get(); IManagementCrudService<SharedLibraryBean> library_db = this._service_context.getCoreManagementDbService() .getSharedLibraryStore(); library_db.deleteDatastore().get(); final ObjectMapper mapper = BeanTemplateUtils.configureMapper(Optional.empty()); final ObjectNode v1_share_1 = (ObjectNode) mapper .readTree(this.getClass().getResourceAsStream("test_v1_sync_sample_share.json")); final DBObject v1_share_1_dbo = (DBObject) JSON.parse(v1_share_1.toString()); v1_share_1_dbo.put("_id", new ObjectId(v1_share_1.get("_id").asText())); assertEquals(0L, (long) v1_share_db.countObjects().get()); dbc.save(v1_share_1_dbo); //v1_share_db.storeObjects(Arrays.asList(v1_share_1)).get(); assertEquals(1L, (long) v1_share_db.countObjects().get()); final SharedLibraryBean share1 = IkanowV1SyncService_LibraryJars.getLibraryBeanFromV1Share(v1_share_1); assertEquals(0L, (long) library_db.countObjects().get()); library_db.storeObjects(Arrays.asList(share1)).get(); assertEquals(1L, (long) library_db.countObjects().get()); // No error - create { final ManagementFuture<?> test_1 = FutureUtils.createManagementFuture( CompletableFuture.completedFuture(Unit.unit()), CompletableFuture.completedFuture(Arrays.asList(ErrorUtils.buildSuccessMessage("", "", "", ""))) // (single non error) ); final CompletableFuture<Boolean> res = IkanowV1SyncService_LibraryJars.updateV1ShareErrorStatus_top( "555d44e3347d336b3e8c4cbe", test_1, library_db, v1_share_db, true); assertEquals(false, res.get()); ObjectNode unchanged = (ObjectNode) v1_share_db.getRawService() .getObjectById(new ObjectId("555d44e3347d336b3e8c4cbe")).get().get(); assertEquals(v1_share_1.without("_id").toString(), unchanged.without("_id").toString()); } // DB call throws exception { final CompletableFuture<?> error_out = new CompletableFuture<>(); error_out.completeExceptionally(new RuntimeException("test")); final ManagementFuture<?> test_1 = FutureUtils.createManagementFuture(error_out); final CompletableFuture<Boolean> res = IkanowV1SyncService_LibraryJars.updateV1ShareErrorStatus_top( "555d44e3347d336b3e8c4cbe", test_1, library_db, v1_share_db, true); assertEquals(true, res.get()); JsonNode changed = v1_share_db.getRawService().getObjectById(new ObjectId("555d44e3347d336b3e8c4cbe")) .get().get(); assertTrue(changed.get("description").asText() .contains("] (unknown) ((unknown)): ERROR: [java.lang.RuntimeException: test")); // This shouldn't yet pe present assertFalse("Description error time travels: " + changed.get("description").asText(), changed.get("description").asText().contains("] (test) (unknown): ERROR: test")); } // db call throws exception, object doesn't exist (code coverage!) { final CompletableFuture<?> error_out = new CompletableFuture<>(); error_out.completeExceptionally(new RuntimeException("test")); final ManagementFuture<?> test_1 = FutureUtils.createManagementFuture(error_out); final CompletableFuture<Boolean> res = IkanowV1SyncService_LibraryJars.updateV1ShareErrorStatus_top( "555d44e3347d336b3e8c4cbf", test_1, library_db, v1_share_db, true); assertEquals(false, res.get()); } // User errors (+update not create) { final ManagementFuture<?> test_1 = FutureUtils.createManagementFuture( CompletableFuture.completedFuture(Unit.unit()), CompletableFuture.completedFuture( Arrays.asList(ErrorUtils.buildErrorMessage("test", "test", "test", "test"))) // (single non error) ); final CompletableFuture<Boolean> res = IkanowV1SyncService_LibraryJars.updateV1ShareErrorStatus_top( "555d44e3347d336b3e8c4cbe", test_1, library_db, v1_share_db, false); assertEquals(true, res.get()); JsonNode changed = v1_share_db.getRawService().getObjectById(new ObjectId("555d44e3347d336b3e8c4cbe")) .get().get(); SharedLibraryBean v2_version = library_db.getObjectById("v1_555d44e3347d336b3e8c4cbe").get().get(); assertTrue("v2 lib bean needed updating: " + v2_version.modified(), new Date().getTime() - v2_version.modified().getTime() < 5000L); // Still has the old error assertTrue("Description missing errors: " + changed.get("description").asText(), changed.get("description").asText() .contains("] (unknown) ((unknown)): ERROR: [java.lang.RuntimeException: test")); // Now has the new error assertTrue("Description missing errors: " + changed.get("description").asText(), changed.get("description").asText().contains("] test (test): ERROR: test")); } }
From source file:io.pravega.controller.store.stream.InMemoryStream.java
@Override CompletableFuture<Void> removeMarkerData(int segmentNumber) { synchronized (markersLock) { markers.remove(segmentNumber);/*from w w w .jav a 2 s.c om*/ } return CompletableFuture.completedFuture(null); }
From source file:com.ikanow.aleph2.shared.crud.mongodb.services.MongoDbCrudService.java
@Override public CompletableFuture<Long> updateObjectsBySpec(final QueryComponent<O> spec, final Optional<Boolean> upsert, final UpdateComponent<O> update) { try {//from w ww. ja v a 2 s. co m final Tuple2<DBObject, DBObject> query_and_meta = MongoDbUtils.convertToMongoQuery(spec); final DBObject update_object = MongoDbUtils.createUpdateObject(update); final WriteResult<O, K> wr = _state.coll.update(query_and_meta._1(), update_object, upsert.orElse(false), true); return CompletableFuture.completedFuture((Long) (long) wr.getN()); } catch (Exception e) { return FutureUtils.<Long>returnError(e); } }
From source file:io.pravega.controller.store.stream.InMemoryStream.java
@Override CompletableFuture<Data<Integer>> getMarkerData(int segmentNumber) { synchronized (markersLock) { if (!markers.containsKey(segmentNumber)) { return FutureHelpers.failedFuture(StoreException.create(StoreException.Type.DATA_NOT_FOUND, "Stream: " + getName() + " Segment: " + segmentNumber)); }/* w ww . j a va 2s. c o m*/ return CompletableFuture.completedFuture(copy(markers.get(segmentNumber))); } }
From source file:io.flutter.inspector.DiagnosticsNode.java
private CompletableFuture<String> createPropertyDocFurure() { final DiagnosticsNode parent = getParent(); if (parent != null) { return inspectorService.toDartVmServiceValueForSourceLocation(parent.getValueRef()) .thenComposeAsync((DartVmServiceValue vmValue) -> { if (vmValue == null) { return CompletableFuture.completedFuture(null); }/*from w ww . j a v a 2 s . c om*/ return inspectorService.getPropertyLocation(vmValue.getInstanceRef(), getName()) .thenApplyAsync((XSourcePosition sourcePosition) -> { if (sourcePosition != null) { final VirtualFile file = sourcePosition.getFile(); final int offset = sourcePosition.getOffset(); final Project project = getProject(file); if (project != null) { final List<HoverInformation> hovers = DartAnalysisServerService .getInstance(project).analysis_getHover(file, offset); if (!hovers.isEmpty()) { return hovers.get(0).getDartdoc(); } } } return "Unable to find property source"; }); }); } return CompletableFuture.completedFuture("Unable to find property source"); }
From source file:com.ikanow.aleph2.management_db.mongodb.services.IkanowV1SyncService_Buckets.java
/** Update a bucket based on a new V1 source * @param key/*from ww w . java2 s . co m*/ * @param bucket_mgmt * @param underlying_bucket_status_mgmt * @param source_db * @return */ @SuppressWarnings("unchecked") protected static ManagementFuture<Supplier<Object>> updateBucket(final String key, final IManagementCrudService<DataBucketBean> bucket_mgmt, final IManagementCrudService<DataBucketStatusBean> underlying_bucket_status_mgmt, final ICrudService<JsonNode> source_db) { _logger.info(ErrorUtils.get("Source {0} was modified, updating bucket", key)); // Get the full source from V1 // .. and from V2: the existing bucket and the existing status // OK first off, we're immediately going to update the bucket's modified time // since otherwise if the update fails then we'll get stuck updating it every iteration... // (ie this is the reason we set isApproved:false in the create case) //(this ugliness just handles the test case already running on the underlying service) final ICrudService<DataBucketBean> underlying_bucket_db = bucket_mgmt .getUnderlyingPlatformDriver(ICrudService.class, Optional.empty()).orElse(bucket_mgmt); underlying_bucket_db.updateObjectById(getBucketIdFromV1SourceKey(key), CrudUtils.update(DataBucketBean.class).set(DataBucketBean::modified, new Date())); final SingleQueryComponent<JsonNode> v1_query = CrudUtils.allOf().when("key", key); final CompletableFuture<Optional<JsonNode>> f_v1_source = source_db.getObjectBySpec(v1_query); return FutureUtils.denestManagementFuture( f_v1_source.<ManagementFuture<Supplier<Object>>>thenApply(Lambdas.wrap_u(v1_source -> { // Once we have all the queries back, get some more information final boolean is_now_suspended = safeJsonGet("searchCycle_secs", v1_source.get()).asInt(1) < 0; final DataBucketBean new_object = getBucketFromV1Source(v1_source.get()); // (Update status in underlying status store so don't trip a spurious harvest call) CompletableFuture<?> update = underlying_bucket_status_mgmt.updateObjectById( getBucketIdFromV1SourceKey(key), CrudUtils.update(DataBucketStatusBean.class) .set(DataBucketStatusBean::suspended, is_now_suspended)); // Then update the management db return FutureUtils.denestManagementFuture( update.thenApply(__ -> bucket_mgmt.storeObject(new_object, true))); })).exceptionally(e -> { return FutureUtils .<Supplier<Object>>createManagementFuture( FutureUtils.returnError(new RuntimeException(e)), CompletableFuture.completedFuture(Arrays.asList(new BasicMessageBean(new Date(), false, "IkanowV1SyncService_Buckets", "updateBucket", null, ErrorUtils.getLongForm("{0}", e), null)))); })); }
From source file:io.pravega.controller.store.stream.InMemoryStream.java
@Override CompletableFuture<Map<String, Data<Integer>>> getCurrentTxns() { synchronized (txnsLock) { Map<String, Data<Integer>> map = activeTxns.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, x -> copy(x.getValue()))); return CompletableFuture.completedFuture(Collections.unmodifiableMap(map)); }/*from w w w. java 2s .c o m*/ }
From source file:io.pravega.controller.store.stream.InMemoryStream.java
@Override CompletableFuture<Void> checkScopeExists() throws StoreException { return CompletableFuture.completedFuture(null); }
From source file:com.ikanow.aleph2.graph.titan.services.TitanGraphService.java
/** Utility method for building the graph indices * @param bucket/*w w w . ja v a2 s .c o m*/ * @return */ public static CompletableFuture<Collection<BasicMessageBean>> createIndices(DataBucketBean bucket, TitanGraph titan, boolean use_es_for_dedup_indices) { final TitanManagement mgmt = titan.openManagement(); // First off, let's ensure that a2_p is indexed: (note these apply to both vertixes and edges) try { final PropertyKey bucket_index = mgmt.makePropertyKey(GraphAnnotationBean.a2_p).dataType(String.class) .cardinality(Cardinality.SET).make(); try { mgmt.buildIndex(GLOBAL_PATH_INDEX_GV, Vertex.class) .addKey(bucket_index, Mapping.STRING.asParameter()).buildMixedIndex(SEARCH_INDEX_NAME); } catch (IllegalArgumentException e) { //DEBUG //_logger.error(ErrorUtils.getLongForm("{0}", e)); //e.printStackTrace(); } // (already indexed, this is fine/expected) try { mgmt.buildIndex(GLOBAL_PATH_INDEX_GE, Edge.class).addKey(bucket_index, Mapping.STRING.asParameter()) .buildMixedIndex(SEARCH_INDEX_NAME); } catch (IllegalArgumentException e) { //DEBUG //_logger.error(ErrorUtils.getLongForm("{0}", e)); //e.printStackTrace(); } // (already indexed, this is fine/expected) } catch (IllegalArgumentException e) { //DEBUG //e.printStackTrace(); } // (already indexed, this is fine/expected) // Created/modified try { mgmt.buildIndex(GLOBAL_CREATED_GV, Vertex.class) .addKey(mgmt.makePropertyKey(GraphAnnotationBean.a2_tc).dataType(Long.class).make()) .buildMixedIndex(SEARCH_INDEX_NAME); } catch (IllegalArgumentException e) { //DEBUG //_logger.error(ErrorUtils.getLongForm("{0}", e)); //e.printStackTrace(); } // (already indexed, this is fine/expected) try { mgmt.buildIndex(GLOBAL_MODIFIED_GV, Vertex.class) .addKey(mgmt.makePropertyKey(GraphAnnotationBean.a2_tm).dataType(Long.class).make()) .buildMixedIndex(SEARCH_INDEX_NAME); } catch (IllegalArgumentException e) { //DEBUG //_logger.error(ErrorUtils.getLongForm("{0}", e)); //e.printStackTrace(); } // (already indexed, this is fine/expected) // Then check that the global default index is set Optional<List<String>> maybe_dedup_fields = Optionals .of(() -> bucket.data_schema().graph_schema().deduplication_fields()); final Collection<BasicMessageBean> ret_val = maybe_dedup_fields.map(dedup_fields -> { //TODO (ALEPH-15): manage the index pointed to by the bucket's signature return Arrays.asList(ErrorUtils.buildErrorMessage(TitanGraph.class.getSimpleName(), "onPublishOrUpdate", ErrorUtils.NOT_YET_IMPLEMENTED, "custom:deduplication_fields")); }).orElseGet(() -> { try { //TODO (ALEPH-15): There's a slightly tricky decision here... // using ES makes dedup actions "very" no longer transactional because of the index refresh // (in theory using Cassandra or HBase makes things transactional, though I haven't tested that) // Conversely not using ES puts more of the load on the smaller Cassandra/HBase clusters // Need to make configurable, but default to using the transactional layer // Of course, if I move to eg unipop later on then I'll have to fix this // It's not really any different to deduplication _except_ it can happen across buckets (unlike dedup) so it's much harder to stop // A few possibilities: // 1) Have a job that runs on new-ish data (ofc that might not be easy to detect) that merges things (ofc very unclear how to do that) // 2) Centralize all insert actions // Ah here's some more interest - looks like Hbase and Cassandra's eventual consistency can cause duplicates: // http://s3.thinkaurelius.com/docs/titan/1.0.0/eventual-consistency.html ... tldr: basically need to have regular "clean up jobs" and live with transient issues // (or use a consistent data store - would also require a decent amount of code here because our dedup strategy is not perfect) final Function<String, PropertyKey> getOrCreateProperty = field -> Optional .ofNullable(mgmt.getPropertyKey(field)) .orElseGet(() -> mgmt.makePropertyKey(field).dataType(String.class).make()); final PropertyKey name_index = getOrCreateProperty.apply(GraphAnnotationBean.name); final PropertyKey type_index = getOrCreateProperty.apply(GraphAnnotationBean.type); if (use_es_for_dedup_indices) { mgmt.buildIndex(GLOBAL_DEFAULT_INDEX_GV, Vertex.class) .addKey(name_index, Mapping.TEXTSTRING.asParameter()) .addKey(type_index, Mapping.STRING.asParameter()).buildMixedIndex(SEARCH_INDEX_NAME); } else { // use the storage backend which should have better consistency properties mgmt.buildIndex(GLOBAL_DEFAULT_INDEX_GV, Vertex.class).addKey(name_index).addKey(type_index) //TODO (ALEPH-15: make this unique()? and then have multiple contexts, either via property or lots of graphs? .buildCompositeIndex(); //(in this case, also index "name" as an ES field to make it easier to search over) mgmt.buildIndex(GLOBAL_DEFAULT_INDEX_GV + "_TEXT", Vertex.class) .addKey(name_index, Mapping.TEXT.asParameter()).buildMixedIndex(SEARCH_INDEX_NAME); } } catch (IllegalArgumentException e) { //DEBUG //_logger.error(ErrorUtils.getLongForm("{0}", e)); //e.printStackTrace(); } // (already indexed, this is fine/expected) return Collections.emptyList(); }); //TODO (ALEPH-15): allow other indexes (etc) via the schema technology override // Complete management transaction mgmt.commit(); //TODO (ALEPH-15): want to complete the future only once the indexing steps are done? return CompletableFuture.completedFuture(ret_val); }