Search in sources :

Example 1 with Retryer

use of org.bboxdb.commons.Retryer in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class OrderManager method placeOrderAndWaitUntilActive.

/**
 * Place an order and retry if Exception occur
 * @param order - new BitfinexOrder to place
 * @throws APIException
 * @throws InterruptedException
 */
public void placeOrderAndWaitUntilActive(final BitfinexOrder order) throws APIException, InterruptedException {
    final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
    if (!capabilities.isHavingOrdersWriteCapability()) {
        throw new APIException("Unable to wait for order " + order + " connection has not enough capabilities: " + capabilities);
    }
    order.setApikey(bitfinexApiBroker.getApiKey());
    final Callable<Boolean> orderCallable = () -> placeOrderOrderOnAPI(order);
    // Bitfinex does not implement a happens-before relationship. Sometimes
    // canceling a stop-loss order and placing a new stop-loss order results
    // in an 'ERROR, reason is Invalid order: not enough exchange balance'
    // error for some seconds. The retryer tries to place the order up to
    // three times
    final Retryer<Boolean> retryer = new Retryer<>(ORDER_RETRIES, RETRY_DELAY_IN_MS, orderCallable);
    retryer.execute();
    if (retryer.getNeededExecutions() > 1) {
        logger.info("Nedded {} executions for placing the order", retryer.getNeededExecutions());
    }
    if (!retryer.isSuccessfully()) {
        final Exception lastException = retryer.getLastException();
        if (lastException == null) {
            throw new APIException("Unable to execute order");
        } else {
            throw new APIException(lastException);
        }
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities) Retryer(org.bboxdb.commons.Retryer) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException)

Example 2 with Retryer

use of org.bboxdb.commons.Retryer in project bboxdb by jnidzwetzki.

the class DistributionRegionSyncer method updateNode.

/**
 * Update the given node
 * @param nodePath
 * @param region
 * @throws InterruptedException
 */
private void updateNode(final String nodePath, final DistributionRegion region) throws InterruptedException {
    logger.debug("updateNode called with node {}", nodePath);
    final Watcher callbackWatcher = this;
    final Retryer<Boolean> retryer = new Retryer<>(10, 100, () -> {
        try {
            final Collection<BBoxDBInstance> systemsForDistributionRegion = distributionRegionAdapter.getSystemsForDistributionRegion(region);
            region.setSystems(systemsForDistributionRegion);
            final int regionId = distributionGroupAdapter.getRegionIdForPath(nodePath);
            if (region.getRegionId() != regionId) {
                throw new RuntimeException("Replacing region id " + region.getRegionId() + " with " + regionId + " on " + nodePath);
            }
            final DistributionRegionState regionState = distributionRegionAdapter.getStateForDistributionRegion(nodePath, callbackWatcher);
            region.setState(regionState);
            updateChildrenForRegion(nodePath, region);
        } catch (ZookeeperNotFoundException e) {
            // Node is deleted, let the deletion callback remove the node
            logger.debug("Skippping node update for path {}, node is deleted", nodePath);
        }
        return true;
    });
    retryer.execute();
    if (!retryer.isSuccessfully()) {
        logger.error("Got error while rereading tree", retryer.getLastException());
    }
}
Also used : Retryer(org.bboxdb.commons.Retryer) ZookeeperNotFoundException(org.bboxdb.distribution.zookeeper.ZookeeperNotFoundException) Watcher(org.apache.zookeeper.Watcher) DistributionRegionState(org.bboxdb.distribution.partitioner.DistributionRegionState) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance)

Example 3 with Retryer

use of org.bboxdb.commons.Retryer in project bboxdb by jnidzwetzki.

the class DistributionRegionHelper method getDistributionRegionForNamePrefix.

/**
 * Find the region for the given name prefix
 * @param searchNameprefix
 * @return
 * @throws InterruptedException
 */
public static DistributionRegion getDistributionRegionForNamePrefix(final DistributionRegion region, final long searchNameprefix) throws InterruptedException {
    if (region == null) {
        return null;
    }
    final Callable<DistributionRegion> getDistributionRegion = new Callable<DistributionRegion>() {

        @Override
        public DistributionRegion call() throws Exception {
            return region.getThisAndChildRegions().stream().filter(r -> r.getRegionId() == searchNameprefix).findAny().orElseThrow(() -> new Exception("Unable to get distribution region"));
        }
    };
    // Retry the operation if needed
    final Retryer<DistributionRegion> retyer = new Retryer<>(Const.OPERATION_RETRY, 250, getDistributionRegion);
    retyer.execute();
    return retyer.getResult();
}
Also used : OutdatedDistributionRegion(org.bboxdb.distribution.OutdatedDistributionRegion) Retryer(org.bboxdb.commons.Retryer) Callable(java.util.concurrent.Callable) ZookeeperException(org.bboxdb.distribution.zookeeper.ZookeeperException) BBoxDBException(org.bboxdb.misc.BBoxDBException)

Example 4 with Retryer

use of org.bboxdb.commons.Retryer in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class OrderManager method cancelOrderAndWaitForCompletion.

/**
 * Cancel a order
 * @param id
 * @throws APIException, InterruptedException
 */
public void cancelOrderAndWaitForCompletion(final long id) throws APIException, InterruptedException {
    final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
    if (!capabilities.isHavingOrdersWriteCapability()) {
        throw new APIException("Unable to cancel order " + id + " connection has not enough capabilities: " + capabilities);
    }
    final Callable<Boolean> orderCallable = () -> cancelOrderOnAPI(id);
    // See comment in placeOrder()
    final Retryer<Boolean> retryer = new Retryer<>(ORDER_RETRIES, RETRY_DELAY_IN_MS, orderCallable);
    retryer.execute();
    if (retryer.getNeededExecutions() > 1) {
        logger.info("Nedded {} executions for canceling the order", retryer.getNeededExecutions());
    }
    if (!retryer.isSuccessfully()) {
        final Exception lastException = retryer.getLastException();
        if (lastException == null) {
            throw new APIException("Unable to cancel order");
        } else {
            throw new APIException(lastException);
        }
    }
}
Also used : APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) ConnectionCapabilities(com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities) Retryer(org.bboxdb.commons.Retryer) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException)

Aggregations

Retryer (org.bboxdb.commons.Retryer)4 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)2 ConnectionCapabilities (com.github.jnidzwetzki.bitfinex.v2.entity.ConnectionCapabilities)2 Callable (java.util.concurrent.Callable)1 Watcher (org.apache.zookeeper.Watcher)1 OutdatedDistributionRegion (org.bboxdb.distribution.OutdatedDistributionRegion)1 BBoxDBInstance (org.bboxdb.distribution.membership.BBoxDBInstance)1 DistributionRegionState (org.bboxdb.distribution.partitioner.DistributionRegionState)1 ZookeeperException (org.bboxdb.distribution.zookeeper.ZookeeperException)1 ZookeeperNotFoundException (org.bboxdb.distribution.zookeeper.ZookeeperNotFoundException)1 BBoxDBException (org.bboxdb.misc.BBoxDBException)1