@Override public long getEpoch() { ensurePreconditions(); synchronized (this.entries) { return this.epoch; } }
@Override public void disable() throws DurableDataLogException { ensurePreconditions(); synchronized (this.entries) { this.entries.disable(this.clientId); } close(); }
@Override public CloseableIterator<ReadItem, DurableDataLogException> getReader() throws DurableDataLogException { ensurePreconditions(); return new ReadResultIterator(this.entries.iterator()); }
@Override public CompletableFuture<Void> truncate(LogAddress upToAddress, Duration timeout) { ensurePreconditions(); return CompletableFuture.runAsync(() -> { try { synchronized (this.entries) { this.entries.truncate(upToAddress.getSequence(), this.clientId); } } catch (DataLogWriterNotPrimaryException ex) { throw new CompletionException(ex); } }, this.executorService); }
@Override public CompletableFuture<LogAddress> append(ArrayView data, Duration timeout) { ensurePreconditions(); CompletableFuture<LogAddress> result; try { Entry entry = new Entry(data); synchronized (this.entries) { entry.sequenceNumber = this.offset; this.entries.add(entry, clientId); // Only update internals after a successful add. this.offset += entry.data.length; } result = CompletableFuture.completedFuture(new InMemoryLogAddress(entry.sequenceNumber)); } catch (Throwable ex) { return Futures.failedFuture(ex); } Duration delay = this.appendDelayProvider.get(); if (delay.compareTo(Duration.ZERO) <= 0) { // No delay, execute right away. return result; } else { // Schedule the append after the given delay. return result.thenComposeAsync( logAddress -> Futures.delayedFuture(delay, this.executorService) .thenApply(ignored -> logAddress), this.executorService); } }