Search in sources :

Example 1 with Error

use of org.tikv.kvproto.Errorpb.Error in project client-java by tikv.

the class ImporterClient method ingestWithRetry.

private void ingestWithRetry(Object writeResponse, BackOffer backOffer) {
    try {
        clientLeader.multiIngest(region.getLeaderContext(), writeResponse);
    } catch (RegionException e) {
        logger.warn("ingest failed.", e);
        boolean retry = false;
        Error error = e.getRegionErr();
        if (error != null) {
            if (error.hasNotLeader()) {
                retry = true;
                long newStoreId = error.getNotLeader().getLeader().getStoreId();
                // update Leader here
                logger.warn(String.format("NotLeader Error with region id %d and store id %d, new store id %d", region.getId(), region.getLeader().getStoreId(), newStoreId));
                BackOffFunction.BackOffFuncType backOffFuncType;
                if (newStoreId != NO_LEADER_STORE_ID) {
                    long regionId = region.getId();
                    region = tiSession.getRegionManager().updateLeader(region, newStoreId);
                    if (region == null) {
                        // epoch is not changed, getRegionById is faster than getRegionByKey
                        region = tiSession.getRegionManager().getRegionById(regionId);
                    }
                    backOffFuncType = BackOffFunction.BackOffFuncType.BoUpdateLeader;
                } else {
                    logger.info(String.format("Received zero store id, from region %d try next time", region.getId()));
                    tiSession.getRegionManager().invalidateRegion(region);
                    region = tiSession.getRegionManager().getRegionById(region.getId());
                    backOffFuncType = BackOffFunction.BackOffFuncType.BoRegionMiss;
                }
                backOffer.doBackOff(backOffFuncType, e);
                init();
            } else if (error.hasServerIsBusy()) {
                retry = true;
                // this error is reported from kv:
                // will occur when write pressure is high. Please try later.
                logger.warn(String.format("Server is busy for region [%s], reason: %s", region, error.getServerIsBusy().getReason()));
                backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoServerBusy, new StatusRuntimeException(Status.fromCode(Status.Code.UNAVAILABLE).withDescription(error.toString())));
            } else {
                tiSession.getRegionManager().invalidateRegion(region);
            }
        }
        if (retry) {
            ingestWithRetry(writeResponse, backOffer);
        } else {
            throw e;
        }
    }
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) Error(org.tikv.kvproto.Errorpb.Error) RegionException(org.tikv.common.exception.RegionException)

Aggregations

StatusRuntimeException (io.grpc.StatusRuntimeException)1 RegionException (org.tikv.common.exception.RegionException)1 Error (org.tikv.kvproto.Errorpb.Error)1