com.twitter.util.ExceptionalFunction Java Examples

The following examples show how to use com.twitter.util.ExceptionalFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TerrapinClient.java    From terrapin with Apache License 2.0 6 votes vote down vote up
private ExceptionalFunction<TerrapinResponse, TerrapinSingleResponse> getManyToGetOneFunction(
    final ByteBuffer key) {
  return new ExceptionalFunction<TerrapinResponse, TerrapinSingleResponse>() {
      @Override
      public TerrapinSingleResponse applyE(TerrapinResponse response) throws Throwable {
        TerrapinSingleResponse singleResponse = response.getResponseMap().get(key);
        if (singleResponse == null) {
          return new TerrapinSingleResponse();
        } else {
          if (singleResponse.isSetErrorCode()) {
            throw new TerrapinGetException().setErrorCode(singleResponse.getErrorCode());
          } else {
            return singleResponse;
          }
        }
      }
    };
}
 
Example #2
Source File: BKLogReadHandler.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
/**
 * Elective stream lock--readers are not required to acquire the lock before using the stream.
 */
synchronized Future<Void> lockStream() {
    if (null == lockAcquireFuture) {
        final Function0<DistributedLock> lockFunction =  new ExceptionalFunction0<DistributedLock>() {
            @Override
            public DistributedLock applyE() throws IOException {
                // Unfortunately this has a blocking call which we should not execute on the
                // ZK completion thread
                BKLogReadHandler.this.readLock = new ZKDistributedLock(
                        lockStateExecutor,
                        lockFactory,
                        readLockPath,
                        conf.getLockTimeoutMilliSeconds(),
                        statsLogger.scope("read_lock"));

                LOG.info("acquiring readlock {} at {}", getLockClientId(), readLockPath);
                return BKLogReadHandler.this.readLock;
            }
        };
        lockAcquireFuture = ensureReadLockPathExist().flatMap(new ExceptionalFunction<Void, Future<Void>>() {
            @Override
            public Future<Void> applyE(Void in) throws Throwable {
                return scheduler.apply(lockFunction).flatMap(new ExceptionalFunction<DistributedLock, Future<Void>>() {
                    @Override
                    public Future<Void> applyE(DistributedLock lock) throws IOException {
                        return acquireLockOnExecutorThread(lock);
                    }
                });
            }
        });
    }
    return lockAcquireFuture;
}
 
Example #3
Source File: BKDistributedLogManager.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
<T> Future<T> processReaderOperation(final Function<BKLogReadHandler, Future<T>> func) {
    initializeFuturePool(false);
    return readerFuturePool.apply(new ExceptionalFunction0<BKLogReadHandler>() {
        @Override
        public BKLogReadHandler applyE() throws Throwable {
            return getReadHandlerForListener(true);
        }
    }).flatMap(new ExceptionalFunction<BKLogReadHandler, Future<T>>() {
        @Override
        public Future<T> applyE(final BKLogReadHandler readHandler) throws Throwable {
            return func.apply(readHandler);
        }
    });
}
 
Example #4
Source File: TestAsyncReaderLock.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testReaderLockCloseInAcquireCallback() throws Exception {
    final String name = runtime.getMethodName();
    DistributedLogManager dlm = createNewDLM(conf, name);
    BKAsyncLogWriter writer = (BKAsyncLogWriter)(dlm.startAsyncLogSegmentNonPartitioned());
    writer.write(DLMTestUtil.getLogRecordInstance(1L));
    writer.closeAndComplete();

    final CountDownLatch latch = new CountDownLatch(1);

    Future<AsyncLogReader> futureReader1 = dlm.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
    futureReader1.flatMap(new ExceptionalFunction<AsyncLogReader, Future<Void>>() {
        @Override
        public Future<Void> applyE(AsyncLogReader reader) throws IOException {
            return reader.asyncClose().map(new AbstractFunction1<Void, Void>() {
                @Override
                public Void apply(Void result) {
                    latch.countDown();
                    return null;
                }
            });
        }
    });

    latch.await();
    dlm.close();
}
 
Example #5
Source File: PinLaterBackendBase.java    From pinlater with Apache License 2.0 5 votes vote down vote up
protected void initialize() throws Exception {
  if (this.shardConfigFilePath != null) {
    String fullFilePath = getClass().getResource("/" + shardConfigFilePath).getPath();
    ConfigFileWatcher.defaultInstance().addWatch(
        fullFilePath, new ExceptionalFunction<byte[], Void>() {
      @Override
      public synchronized Void applyE(byte[] bytes) throws Exception {
        processConfigUpdate(bytes);
        return null;
      }
    });
  }

  // Initialize the future pool we will use to make blocking calls to Redis.
  // We size the future pool such that there is one thread for every available connection.
  int futurePoolSize = configuration.getInt("BACKEND_CONNECTIONS_PER_SHARD") * getShards().size();
  this.futurePool = new ExecutorServiceFuturePool(Executors.newFixedThreadPool(
      futurePoolSize,
      new ThreadFactoryBuilder().setDaemon(true).setNameFormat(
          backendName + "FuturePool-%d").build()));

  // Create a map of queueName -> aync semaphore to control dequeue concurrency.
  // We configure the map to create entries on demand since queues can be created at any time.
  final int dequeueConcurrencyPerQueue =
      configuration.getInt("BACKEND_DEQUEUE_CONCURRENCY_PER_QUEUE_PER_SHARD") * getShards()
          .size();
  // We set maxWaiters on the async semaphore to the max concurrency on the server as an
  // additional safety measure.
  final int maxWaiters = configuration.getInt("MAX_CONCURRENT_REQUESTS");
  this.dequeueSemaphoreMap = CacheBuilder.newBuilder().build(
      new CacheLoader<String, AsyncSemaphore>() {
        @Override
        public AsyncSemaphore load(String queueName) throws Exception {
          AsyncSemaphore asyncSemaphore =
              new AsyncSemaphore(dequeueConcurrencyPerQueue, maxWaiters);
          Stats.setGauge("dequeue-semaphore-waiters-" + queueName, asyncSemaphore.numWaiters());
          return asyncSemaphore;
        }
      });
}
 
Example #6
Source File: TerrapinServiceImpl.java    From terrapin with Apache License 2.0 4 votes vote down vote up
@Override
public Future<TerrapinSingleResponse> get(final TerrapinGetRequest request) {
  final long startTimeMillis = System.currentTimeMillis();
  if (request.getClusterList().isEmpty()) {
    return Future.exception(new TerrapinGetException("Cluster list is empty", TerrapinGetErrorCode.INVALID_REQUEST));
  }
  ReplicatedTerrapinClient terrapinClient = getReplicatedTerrapinClient(request.getClusterList());
  if (terrapinClient == null) {
    return Future.exception(new TerrapinGetException(
        "Clusters [" + Joiner.on(", ").join(request.getClusterList()) + "] not found.",
        TerrapinGetErrorCode.CLUSTER_NOT_FOUND));
  }
  RequestOptions options;
  if (request.isSetOptions()) {
    options = request.getOptions();
  } else {
    options = new RequestOptions();
  }
  try {
    return terrapinClient.getMany(request.getFileSet(),
        Sets.newHashSet(ByteBuffer.wrap(request.getKey())), options).map(
            new ExceptionalFunction<TerrapinResponse, TerrapinSingleResponse>() {
              @Override
              public TerrapinSingleResponse applyE(TerrapinResponse response)
                  throws TerrapinGetException {
                ByteBuffer keyBuf = ByteBuffer.wrap(request.getKey());
                if (response.getResponseMap().containsKey(keyBuf)) {
                  TerrapinSingleResponse returnResponse = response.getResponseMap().get(keyBuf);
                  if (returnResponse.isSetErrorCode()) {
                    throw new TerrapinGetException("Read failed.", returnResponse.getErrorCode());
                  } else {
                    Stats.addMetric(request.getFileSet() + "-value-size", returnResponse.getValue().length);
                    Stats.addMetric("value-size", returnResponse.getValue().length);
                    return returnResponse;
                  }
                } else {
                  return new TerrapinSingleResponse();
                }
              }
            }).rescue(new Function<Throwable, Future<TerrapinSingleResponse>>() {
              @Override
              public Future<TerrapinSingleResponse> apply(Throwable t) {
                return getExceptionFuture(t);
              }
            }).ensure(new Function0<BoxedUnit>() {
              @Override
              public BoxedUnit apply() {
                int timeMillis = (int)(System.currentTimeMillis() - startTimeMillis);
                Stats.addMetric(request.getFileSet() + "-lookup-latency-ms", timeMillis);
                Stats.addMetric("lookup-latency-ms", timeMillis);
                return BoxedUnit.UNIT;
              }
          });
  } catch (Exception e) {
    return getExceptionFuture(e);
  }
}
 
Example #7
Source File: PinLaterQueueConfig.java    From pinlater with Apache License 2.0 4 votes vote down vote up
public void initialize() throws Exception {
  // Check if use of serverset is enabled, and if so, register a change monitor so we
  // can find out how many PinLater servers are active. We use this to compute the
  // per-server rate limit.
  if (pinlaterServerSetEnabled) {
    MorePreconditions.checkNotBlank(pinlaterServerSetPath);
    LOG.info("Monitoring pinlater serverset: {}", pinlaterServerSetPath);
    String fullServerSetPath = getClass().getResource("/" + pinlaterServerSetPath).getPath();
    ServerSet serverSet = new ConfigFileServerSet(fullServerSetPath);
    serverSet.monitor(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
      @Override
      public void onChange(ImmutableSet<ServiceInstance> hostSet) {
        int oldSize = numPinLaterServers.get();
        int newSize = hostSet.size();
        if (newSize == 0) {
          LOG.error("PinLater serverset is empty, ignoring and keeping old size: {}", oldSize);
          return;
        }
        if (oldSize == newSize) {
          LOG.info("PinLater serverset update, size unchanged: {}", oldSize);
          return;
        }

        LOG.info("PinLater serverset update, old size: {}, new size: {}", oldSize, newSize);
        numPinLaterServers.set(newSize);
        rebuild();
      }
    });
  } else {
    LOG.info("PinLater server set is disabled; rate limits will be applied per server.");
  }

  if (queueConfigFilePath == null || queueConfigFilePath.isEmpty()) {
    LOG.info("Queue config zookeeper path not specified, using defaults.");
    return;
  }

  LOG.info("Registering watch on queue config: {}", queueConfigFilePath);
  String fullQueueConfigFilePath = getClass().getResource("/" + queueConfigFilePath).getPath();
  ConfigFileWatcher.defaultInstance().addWatch(
      fullQueueConfigFilePath, new ExceptionalFunction<byte[], Void>() {
        @Override
        public Void applyE(byte[] bytes) throws Exception {
          QueueConfigSchema queueConfigSchema = QueueConfigSchema.load(bytes);
          LOG.info("Queue config update, new value: {}", queueConfigSchema);
          queueConfigSchemaRef.set(queueConfigSchema);
          rebuild();
          return null;
        }
      });
}