@Override public long parseLong(String value, boolean roundUp, LongSupplier now) { Number n; try { n = format.parse(value); } catch (ParseException e) { throw new RuntimeException(e); } if (format.isParseIntegerOnly()) { return n.longValue(); } else { double d = n.doubleValue(); if (roundUp) { d = Math.ceil(d); } else { d = Math.floor(d); } return Math.round(d); } }
private TranslogWriter( final ChannelFactory channelFactory, final ShardId shardId, final Checkpoint initialCheckpoint, final FileChannel channel, final Path path, final ByteSizeValue bufferSize, final LongSupplier globalCheckpointSupplier) throws IOException { super(initialCheckpoint.generation, channel, path, channel.position()); this.shardId = shardId; this.channelFactory = channelFactory; this.outputStream = new BufferedChannelOutputStream(java.nio.channels.Channels.newOutputStream(channel), bufferSize.bytesAsInt()); this.lastSyncedCheckpoint = initialCheckpoint; this.totalOffset = initialCheckpoint.offset; assert initialCheckpoint.minSeqNo == SequenceNumbersService.NO_OPS_PERFORMED : initialCheckpoint.minSeqNo; this.minSeqNo = initialCheckpoint.minSeqNo; assert initialCheckpoint.maxSeqNo == SequenceNumbersService.NO_OPS_PERFORMED : initialCheckpoint.maxSeqNo; this.maxSeqNo = initialCheckpoint.maxSeqNo; this.globalCheckpointSupplier = globalCheckpointSupplier; }
public long parse(String text, LongSupplier now, boolean roundUp, DateTimeZone timeZone) { long time; String mathString; if (text.startsWith("now")) { try { time = now.getAsLong(); } catch (Exception e) { throw new ElasticsearchParseException("could not read the current timestamp", e); } mathString = text.substring("now".length()); } else { int index = text.indexOf("||"); if (index == -1) { return parseDateTime(text, timeZone, roundUp); } time = parseDateTime(text.substring(0, index), timeZone, false); mathString = text.substring(index + 2); } return parseMath(mathString, time, roundUp, timeZone); }
public TransportBulkAction(Settings settings, ThreadPool threadPool, TransportService transportService, ClusterService clusterService, IngestService ingestService, TransportShardBulkAction shardBulkAction, TransportCreateIndexAction createIndexAction, ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, AutoCreateIndex autoCreateIndex, LongSupplier relativeTimeProvider) { super(settings, BulkAction.NAME, threadPool, transportService, actionFilters, indexNameExpressionResolver, BulkRequest::new); Objects.requireNonNull(relativeTimeProvider); this.clusterService = clusterService; this.ingestService = ingestService; this.shardBulkAction = shardBulkAction; this.createIndexAction = createIndexAction; this.autoCreateIndex = autoCreateIndex; this.relativeTimeProvider = relativeTimeProvider; this.ingestForwarder = new IngestActionForwarder(transportService); clusterService.addStateApplier(this.ingestForwarder); }
TestTransportBulkAction( Settings settings, ThreadPool threadPool, TransportService transportService, ClusterService clusterService, TransportShardBulkAction shardBulkAction, TransportCreateIndexAction createIndexAction, ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, AutoCreateIndex autoCreateIndex, LongSupplier relativeTimeProvider) { super( settings, threadPool, transportService, clusterService, null, shardBulkAction, createIndexAction, actionFilters, indexNameExpressionResolver, autoCreateIndex, relativeTimeProvider); }
/** * Create a new {@code Random64} instance, where the random numbers are * generated by the given long {@code supplier}. * * @param supplier the random number supplier * @return a new {@code Random64} instance * @throws java.lang.NullPointerException if the given {@code supplier} is * {@code null}. */ public static Random64 of(final LongSupplier supplier) { Objects.requireNonNull(supplier); return new Random64() { private static final long serialVersionUID = 1L; private final Boolean _sentry = Boolean.TRUE; @Override public long nextLong() { return supplier.getAsLong(); } @Override public void setSeed(final long seed) { if (_sentry != null) { throw new UnsupportedOperationException( "The 'setSeed(long)' method is not supported." ); } } }; }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<String, Long> cache = null; final Supplier<String> keySupplier = () -> "key"; final LongSupplier supplier = () -> 123L; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedLongSupplierMemoizer<>(cache, keySupplier, supplier); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullKeySupplier() { // given final ConcurrentMap<String, Long> cache = new ConcurrentHashMap<>(); final Supplier<String> keySupplier = null; final LongSupplier supplier = () -> 123L; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide a key function, might just be 'MemoizationDefaults.defaultKeySupplier()'."); // then new ConcurrentMapBasedLongSupplierMemoizer<>(cache, keySupplier, supplier); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullValueSupplier() { // given final ConcurrentMap<String, Long> cache = new ConcurrentHashMap<>(); final Supplier<String> keySupplier = () -> "key"; final LongSupplier supplier = null; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Cannot memoize a NULL Supplier - provide an actual Supplier to fix this."); // then new ConcurrentMapBasedLongSupplierMemoizer<>(cache, keySupplier, supplier); }
/** * */ @Test public void shouldUseSuppliedKey() { // given final ConcurrentMap<String, Long> cache = new ConcurrentHashMap<>(); final Supplier<String> keySupplier = () -> "key"; final LongSupplier supplier = () -> 123L; // when final ConcurrentMapBasedLongSupplierMemoizer<String> memoizer = new ConcurrentMapBasedLongSupplierMemoizer<>( cache, keySupplier, supplier); // then Assert.assertTrue("Cache is not empty before memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoized value does not match expectations", 123L, memoizer.getAsLong()); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", "key", memoizer.viewCacheForTest().keySet().iterator().next()); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.BOXING) public void shouldTriggerOnce() { // given final ConcurrentMap<String, Long> cache = new ConcurrentHashMap<>(); final Supplier<String> keySupplier = () -> "key"; final LongSupplier supplier = mock(LongSupplier.class); given(supplier.getAsLong()).willReturn(123L); // when final ConcurrentMapBasedLongSupplierMemoizer<String> memoizer = new ConcurrentMapBasedLongSupplierMemoizer<>( cache, keySupplier, supplier); // then Assert.assertEquals("Memoized value does not match expectations", 123L, memoizer.getAsLong()); // triggers Assert.assertEquals("Memoized value does not match expectations", 123L, memoizer.getAsLong()); // memoized Assert.assertEquals("Memoized value does not match expectations", 123L, memoizer.getAsLong()); // memoized Assert.assertEquals("Memoized value does not match expectations", 123L, memoizer.getAsLong()); // memoized verify(supplier, times(1)).getAsLong(); // real supplier triggered once, all other calls were memoized }
private void shouldReplyToOnNotLeaderWith( final IntSupplier libraryId, final LongSupplier connectCorrelationId, final String... channels) { whenPolled() .then( (inv) -> { library.onNotLeader(libraryId.getAsInt(), connectCorrelationId.getAsLong(), LEADER_CHANNEL); return 1; }) .then(replyWithApplicationHeartbeat()) .then(noReply()); newLibraryPoller(CLUSTER_CHANNELS); library.startConnecting(); pollTwice(); poll(); attemptToConnectTo(channels); verify(connectHandler).onConnect(fixLibrary); }
/** Creates a `Box.Long` from a `LongSupplier` and a `LongConsumer`. */ public static Lng from(LongSupplier getter, LongConsumer setter) { return new Lng() { @Override public long getAsLong() { return getter.getAsLong(); } @Override public void set(long value) { setter.accept(value); } @Override public String toString() { return "Box.Long.from[" + get() + "]"; } }; }
@Override public synchronized void createCustomMetric(String name, String description, String kind, LongSupplier value) { LongSupplier supplier = requireNonNull(value); Metric cm = context.getMetrics().createCustomMetric( requireNonNull(name), requireNonNull(description), Metric.Kind.valueOf(kind.toUpperCase(Locale.US))); cm.setValue(supplier.getAsLong()); if (metrics == null) { metrics = new ArrayList<>(); metricsGetter = getScheduledExecutorService().scheduleWithFixedDelay(this::updateMetrics, 1, 1, TimeUnit.SECONDS); } metrics.add(() -> cm.setValue(supplier.getAsLong())); }
private void awaitOnLatch(CountDownLatch countDownLatch, LongSupplier nanosRemaining) throws TimeoutException { boolean interrupted = Thread.interrupted(); try { while (true) { try { if (countDownLatch.await(nanosRemaining.getAsLong(), TimeUnit.NANOSECONDS)) { if (!entity.isConnected()) { throw new IllegalStateException("Cluster tier manager disconnected"); } else { return; } } else { throw new TimeoutException(); } } catch (InterruptedException e) { interrupted = true; } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
public void send2Queue(Path inputFile, int batchSize, LongSupplier sleep) throws IOException, InterruptedException { BlockingQueue<String> dataQueue = new ArrayBlockingQueue<>(10000); for (int i = 0; i < 3; i++) { new PushThread(dataQueue).start(); } try (BufferedReader reader = Files.newBufferedReader(inputFile)) { String line; int batchCnt = 0; while ((line = reader.readLine()) != null) { dataQueue.put(processData(line)); if (++batchCnt == batchSize) { batchCnt = 0; long ms = sleep.getAsLong(); if (ms > 0) { Utils.sleep(ms); } } } } finally { dataQueue.put(END); } }
public void send2Queue(Path inputFile, LongSupplier sleep) throws IOException { Jedis jedis = new Jedis(host, port); int counter = 0; try (BufferedReader reader = Files.newBufferedReader(inputFile)) { String line = null; while (line != null || (line = reader.readLine()) != null) { long ms = sleep.getAsLong(); if (ms > 0) { Utils.sleep(ms); } if (jedis.llen(queueName) < maxPaddingSize) { String data = counter++ + "|" + System.currentTimeMillis() + "|" + line; jedis.rpush(queueName, data); line = null; } } } finally { jedis.quit(); } }
@Test public void optional_long_orElseGet() { OptionalLong optionalLong = OptionalLong.empty(); assertEquals(10, optionalLong.orElseGet(() -> 10), 0); // or LongSupplier longSupplier = new LongSupplier() { @Override public long getAsLong() { return 10; } }; assertEquals(10, optionalLong.orElseGet(longSupplier), 0); }
@Test public void testCheckedLongSupplier() { final CheckedLongSupplier longSupplier = () -> { throw new Exception("long"); }; LongSupplier s1 = Unchecked.longSupplier(longSupplier); LongSupplier s2 = CheckedLongSupplier.unchecked(longSupplier); LongSupplier s3 = Sneaky.longSupplier(longSupplier); LongSupplier s4 = CheckedLongSupplier.sneaky(longSupplier); assertLongSupplier(s1, UncheckedException.class); assertLongSupplier(s2, UncheckedException.class); assertLongSupplier(s3, Exception.class); assertLongSupplier(s4, Exception.class); }
public TokenBucketRateLimiter(RDBI rdbi, String keyPrefix, String key, int maxTokens, int refillValue, Duration refillPeriod, LongSupplier clock ) { checkArgument(maxTokens > 0, "Max tokens %s must be > 0", maxTokens); checkArgument(refillValue > 0, "Refill value of %s must be > 0", refillValue); checkArgument(refillPeriod.toMillis() > 0, "Refill period of %ss must be > 0s", refillPeriod.toMillis() / 1000); this.rdbi = rdbi; this.maxTokens = maxTokens; this.refillRatePerMs = refillValue * 1.0 / refillPeriod.toMillis(); fullyQualifiedKey = Joiner.on(":").join(keyPrefix, "tokenBucketRateLimit", key); this.clock = clock; }
private static Map<Class<?>, Class<?>> createPrimitiveSuppliers() { Map<Class<?>, Class<?>> map = new HashMap<>(); map.put(IntSupplier.class, Integer.TYPE); map.put(LongSupplier.class, Long.TYPE); map.put(BooleanSupplier.class, Boolean.TYPE); map.put(DoubleSupplier.class, Double.TYPE); return Collections.unmodifiableMap(map); }
@Override public long parseLong(String value, boolean roundUp, LongSupplier now) { double d = Double.parseDouble(value); if (roundUp) { d = Math.ceil(d); } else { d = Math.floor(d); } return Math.round(d); }
@Override public long parseLong(String value, boolean roundUp, LongSupplier now) { switch (value) { case "false": return 0; case "true": return 1; } throw new IllegalArgumentException("Cannot parse boolean [" + value + "], expected either [true] or [false]"); }
@Override public double parseDouble(String value, boolean roundUp, LongSupplier now) { Number n; try { n = format.parse(value); } catch (ParseException e) { throw new RuntimeException(e); } return n.doubleValue(); }
public QueryShardContext(int shardId, IndexSettings indexSettings, BitsetFilterCache bitsetFilterCache, IndexFieldDataService indexFieldDataService, MapperService mapperService, SimilarityService similarityService, ScriptService scriptService, NamedXContentRegistry xContentRegistry, Client client, IndexReader reader, LongSupplier nowInMillis) { super(indexSettings, mapperService, scriptService, xContentRegistry, client, reader, nowInMillis); this.shardId = shardId; this.indexSettings = indexSettings; this.similarityService = similarityService; this.mapperService = mapperService; this.bitsetFilterCache = bitsetFilterCache; this.indexFieldDataService = indexFieldDataService; this.allowUnmappedFields = indexSettings.isDefaultAllowUnmappedFields(); this.nestedScope = new NestedScope(); }
public QueryRewriteContext(IndexSettings indexSettings, MapperService mapperService, ScriptService scriptService, NamedXContentRegistry xContentRegistry, Client client, IndexReader reader, LongSupplier nowInMillis) { this.mapperService = mapperService; this.scriptService = scriptService; this.indexSettings = indexSettings; this.xContentRegistry = xContentRegistry; this.client = client; this.reader = reader; this.nowInMillis = nowInMillis; }
private Translog openTranslog(EngineConfig engineConfig, IndexWriter writer, LongSupplier globalCheckpointSupplier) throws IOException { assert openMode != null; final TranslogConfig translogConfig = engineConfig.getTranslogConfig(); Translog.TranslogGeneration generation = null; if (openMode == EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG) { generation = loadTranslogIdFromCommit(writer); // We expect that this shard already exists, so it must already have an existing translog else something is badly wrong! if (generation == null) { throw new IllegalStateException("no translog generation present in commit data but translog is expected to exist"); } if (generation.translogUUID == null) { throw new IndexFormatTooOldException("trasnlog", "translog has no generation nor a UUID - this might be an index from a previous version consider upgrading to N-1 first"); } } final Translog translog = new Translog(translogConfig, generation, globalCheckpointSupplier); if (generation == null || generation.translogUUID == null) { assert openMode != EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG : "OpenMode must not be " + EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG; if (generation == null) { logger.debug("no translog ID present in the current generation - creating one"); } else if (generation.translogUUID == null) { logger.debug("upgraded translog to pre 2.0 format, associating translog with index - writing translog UUID"); } boolean success = false; try { commitIndexWriter(writer, translog, openMode == EngineConfig.OpenMode.OPEN_INDEX_CREATE_TRANSLOG ? commitDataAsMap(writer).get(SYNC_COMMIT_ID) : null); success = true; } finally { if (success == false) { IOUtils.closeWhileHandlingException(translog); } } } return translog; }
/** * Creates a new QueryShardContext. The context has not types set yet, if types are required set them via * {@link QueryShardContext#setTypes(String...)}. * * Passing a {@code null} {@link IndexReader} will return a valid context, however it won't be able to make * {@link IndexReader}-specific optimizations, such as rewriting containing range queries. */ public QueryShardContext newQueryShardContext(int shardId, IndexReader indexReader, LongSupplier nowInMillis) { return new QueryShardContext( shardId, indexSettings, indexCache.bitsetFilterCache(), indexFieldData, mapperService(), similarityService(), scriptService, xContentRegistry, client, indexReader, nowInMillis); }
public static TranslogWriter create( ShardId shardId, String translogUUID, long fileGeneration, Path file, ChannelFactory channelFactory, ByteSizeValue bufferSize, final LongSupplier globalCheckpointSupplier) throws IOException { final BytesRef ref = new BytesRef(translogUUID); final int headerLength = getHeaderLength(ref.length); final FileChannel channel = channelFactory.open(file); try { // This OutputStreamDataOutput is intentionally not closed because // closing it will close the FileChannel final OutputStreamDataOutput out = new OutputStreamDataOutput(java.nio.channels.Channels.newOutputStream(channel)); writeHeader(out, ref); channel.force(true); final Checkpoint checkpoint = Checkpoint.emptyTranslogCheckpoint(headerLength, fileGeneration, globalCheckpointSupplier.getAsLong()); writeCheckpoint(channelFactory, file.getParent(), checkpoint); return new TranslogWriter(channelFactory, shardId, checkpoint, channel, file, bufferSize, globalCheckpointSupplier); } catch (Exception exception) { // if we fail to bake the file-generation into the checkpoint we stick with the file and once we recover and that // file exists we remove it. We only apply this logic to the checkpoint.generation+1 any other file with a higher generation is an error condition IOUtils.closeWhileHandlingException(channel); throw exception; } }
/** * Prepares an update request by converting it into an index or delete request or an update response (no action). */ public Result prepare(UpdateRequest request, IndexShard indexShard, LongSupplier nowInMillis) { final GetResult getResult = indexShard.getService().get(request.type(), request.id(), new String[]{RoutingFieldMapper.NAME, ParentFieldMapper.NAME}, true, request.version(), request.versionType(), FetchSourceContext.FETCH_SOURCE); return prepare(indexShard.shardId(), request, getResult, nowInMillis); }
/** Executes bulk item requests and handles request execution exceptions */ static Translog.Location executeBulkItemRequest(IndexMetaData metaData, IndexShard primary, BulkShardRequest request, Translog.Location location, int requestIndex, UpdateHelper updateHelper, LongSupplier nowInMillisSupplier, final MappingUpdatePerformer mappingUpdater) throws Exception { final DocWriteRequest itemRequest = request.items()[requestIndex].request(); final DocWriteRequest.OpType opType = itemRequest.opType(); final BulkItemResultHolder responseHolder; switch (itemRequest.opType()) { case CREATE: case INDEX: responseHolder = executeIndexRequest((IndexRequest) itemRequest, request.items()[requestIndex], primary, mappingUpdater); break; case UPDATE: responseHolder = executeUpdateRequest((UpdateRequest) itemRequest, primary, metaData, request, requestIndex, updateHelper, nowInMillisSupplier, mappingUpdater); break; case DELETE: responseHolder = executeDeleteRequest((DeleteRequest) itemRequest, request.items()[requestIndex], primary); break; default: throw new IllegalStateException("unexpected opType [" + itemRequest.opType() + "] found"); } final BulkItemRequest replicaRequest = responseHolder.replicaRequest; // update the bulk item request because update request execution can mutate the bulk item request request.items()[requestIndex] = replicaRequest; // Modify the replica request, if needed, and return a new translog location location = updateReplicaRequest(responseHolder, opType, location, request); assert replicaRequest.getPrimaryResponse() != null : "replica request must have a primary response"; return location; }
public void testOnlyCallsNowIfNecessary() { final AtomicBoolean called = new AtomicBoolean(); final LongSupplier now = () -> { called.set(true); return 42L; }; parser.parse("2014-11-18T14:27:32", now, false, null); assertFalse(called.get()); parser.parse("now/d", now, false, null); assertTrue(called.get()); }
ClientSideState(final Edition edition, final LongSupplier timeSource, final Supplier<byte[]> ivSource, final Function<Object, byte[]> serialization, final BiFunction<Class<?>, byte[], Object> deserialization, final long timeout) { this.edition = edition; this.timeSource = timeSource; this.ivSource = ivSource; this.serialization = serialization; this.deserialization = deserialization; this.timeout = timeout; }
/** * <p>Creates a binding using the passed supplier and list of dependencies.</p> * * <p>Note that this method requires manual implementation of the respective binding logic. For * most cases, however, the static methods provided by this interface do suffice however and * require far less manually programmed logic.</p> */ @Nonnull static LongBinding create(@Nonnull LongSupplier supplier, ReadOnlyObservable<?>... observables) { return new AbstractLongBinding(new HashSet<>(Arrays.asList(observables))) { @Override protected Long compute() { return supplier.getAsLong(); } }; }
@Override public LongSupplier setLongSupplier(final int id, final LongSupplier supplier) { if (id >= type.getLongStatCount()) { throw new IllegalArgumentException("Id " + id + " is not in range for stat" + type); } return longSuppliers.put(id, supplier); }
@Test public void invokeLongSuppliersShouldUpdateStats() { LongSupplier supplier1 = mock(LongSupplier.class); when(supplier1.getAsLong()).thenReturn(23L); stats.setLongSupplier(4, supplier1); assertEquals(0, stats.invokeSuppliers()); verify(supplier1).getAsLong(); assertEquals(23L, stats.getLong(4)); }
private synchronized long maybeCompute(LongSupplier supplier) { if (!initialized) { value = requireNonNull(supplier.getAsLong()); initialized = true; } return value; }
/** * */ @Test public void shouldMemoizeLongSupplierWithKeyFunction() { // given final LongSupplier supplier = () -> 123L; final Supplier<String> keySupplier = defaultKeySupplier(); // when final LongSupplier memoize = CaffeineMemoize.longSupplier(supplier, keySupplier); // then Assert.assertNotNull("Memoized LongSupplier is NULL", memoize); }
/** * */ @Test public void shouldMemoizeLongSupplier() { // given final LongSupplier supplier = () -> 123L; // when final LongSupplier memoize = CaffeineMemoize.longSupplier(supplier); // then Assert.assertNotNull("Memoized LongSupplier is NULL", memoize); }