View Javadoc

1   package com.atlassian.vcache.internal.core.metrics;
2   
3   import com.atlassian.vcache.ExternalWriteOperationsUnbuffered;
4   import com.atlassian.vcache.PutPolicy;
5   
6   import java.util.concurrent.CompletionStage;
7   
8   import static com.atlassian.vcache.internal.MetricLabel.NUMBER_OF_FAILED_PUT;
9   import static com.atlassian.vcache.internal.MetricLabel.NUMBER_OF_FAILED_REMOVE;
10  import static com.atlassian.vcache.internal.MetricLabel.NUMBER_OF_FAILED_REMOVE_ALL;
11  import static com.atlassian.vcache.internal.MetricLabel.TIMED_PUT_CALL;
12  import static com.atlassian.vcache.internal.MetricLabel.TIMED_REMOVE_ALL_CALL;
13  import static com.atlassian.vcache.internal.MetricLabel.TIMED_REMOVE_CALL;
14  import static com.atlassian.vcache.internal.core.metrics.CacheType.EXTERNAL;
15  import static com.atlassian.vcache.internal.core.metrics.TimedUtils.whenCompletableFuture;
16  
17  /**
18   * Wrapper for a {@link ExternalWriteOperationsUnbuffered} that records metrics.
19   *
20   * @param <V> the value type
21   * @since 1.0.0
22   */
23  abstract class TimedExternalWriteOperationsUnbuffered<V>
24          extends TimedExternalCache<V>
25          implements ExternalWriteOperationsUnbuffered<V> {
26      TimedExternalWriteOperationsUnbuffered(MetricsRecorder metricsRecorder) {
27          super(metricsRecorder);
28      }
29  
30      protected abstract ExternalWriteOperationsUnbuffered<V> getDelegateOps();
31  
32      @Override
33      public CompletionStage<Boolean> put(String key, V value, PutPolicy policy) {
34          try (ElapsedTimer ignored = new ElapsedTimer(
35                  t -> metricsRecorder.record(getDelegate().getName(), EXTERNAL, TIMED_PUT_CALL, t))) {
36              final CompletionStage<Boolean> result = getDelegateOps().put(key, value, policy);
37  
38              whenCompletableFuture(result, future -> {
39                  if (future.isCompletedExceptionally()) {
40                      metricsRecorder.record(getDelegate().getName(), EXTERNAL, NUMBER_OF_FAILED_PUT, 1);
41                  }
42              });
43  
44              return result;
45          }
46      }
47  
48      @Override
49      public CompletionStage<Void> remove(Iterable<String> keys) {
50          try (ElapsedTimer ignored = new ElapsedTimer(
51                  t -> metricsRecorder.record(getDelegate().getName(), EXTERNAL, TIMED_REMOVE_CALL, t))) {
52              final CompletionStage<Void> result = getDelegateOps().remove(keys);
53  
54              whenCompletableFuture(result, future -> {
55                  if (future.isCompletedExceptionally()) {
56                      metricsRecorder.record(getDelegate().getName(), EXTERNAL, NUMBER_OF_FAILED_REMOVE, 1);
57                  }
58              });
59  
60              return result;
61          }
62      }
63  
64      @Override
65      public CompletionStage<Void> removeAll() {
66          try (ElapsedTimer ignored = new ElapsedTimer(
67                  t -> metricsRecorder.record(getDelegate().getName(), EXTERNAL, TIMED_REMOVE_ALL_CALL, t))) {
68              final CompletionStage<Void> result = getDelegateOps().removeAll();
69  
70              whenCompletableFuture(result, future -> {
71                  if (future.isCompletedExceptionally()) {
72                      metricsRecorder.record(getDelegate().getName(), EXTERNAL, NUMBER_OF_FAILED_REMOVE_ALL, 1);
73                  }
74              });
75  
76              return result;
77          }
78      }
79  }