Search in sources :

Example 11 with JedisException

use of redis.clients.jedis.exceptions.JedisException in project gitblit by gitblit.

the class RedisTicketService method createPool.

private JedisPool createPool(String url) {
    JedisPool pool = null;
    if (!StringUtils.isEmpty(url)) {
        try {
            URI uri = URI.create(url);
            if (uri.getScheme() != null && uri.getScheme().equalsIgnoreCase("redis")) {
                int database = Protocol.DEFAULT_DATABASE;
                String password = null;
                if (uri.getUserInfo() != null) {
                    password = uri.getUserInfo().split(":", 2)[1];
                }
                if (uri.getPath().indexOf('/') > -1) {
                    database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
                }
                pool = new JedisPool(new GenericObjectPoolConfig(), uri.getHost(), uri.getPort(), Protocol.DEFAULT_TIMEOUT, password, database);
            } else {
                pool = new JedisPool(url);
            }
        } catch (JedisException e) {
            log.error("failed to create a Redis pool!", e);
        }
    }
    return pool;
}
Also used : JedisException(redis.clients.jedis.exceptions.JedisException) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) URI(java.net.URI)

Example 12 with JedisException

use of redis.clients.jedis.exceptions.JedisException in project cachecloud by sohutv.

the class RedisClusterReshard method migrateSlotData.

/**
     * 指派迁移节点数据
     * CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。
     * CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。
     * CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。
     * MIGRATE host port key destination-db timeout [COPY] [REPLACE]
     * CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
     */
private int migrateSlotData(final Jedis source, final Jedis target, final int slot, boolean isPipelineMigrate) {
    int num = 0;
    final String sourceNodeId = getNodeId(source);
    final String targetNodeId = getNodeId(target);
    boolean isError = false;
    if (sourceNodeId == null || targetNodeId == null) {
        throw new JedisException(String.format("sourceNodeId = %s || targetNodeId = %s", sourceNodeId, targetNodeId));
    }
    boolean isImport = new IdempotentConfirmer() {

        @Override
        public boolean execute() {
            String importing = target.clusterSetSlotImporting(slot, sourceNodeId);
            logger.info("slot={},clusterSetSlotImporting={}", slot, importing);
            return importing != null && importing.equalsIgnoreCase("OK");
        }
    }.run();
    if (!isImport) {
        isError = true;
        logger.error("clusterSetSlotImporting" + failedInfo(target, slot));
    }
    boolean isMigrate = new IdempotentConfirmer() {

        @Override
        public boolean execute() {
            String migrating = source.clusterSetSlotMigrating(slot, targetNodeId);
            logger.info("slot={},clusterSetSlotMigrating={}", slot, migrating);
            return migrating != null && migrating.equalsIgnoreCase("OK");
        }
    }.run();
    if (!isMigrate) {
        isError = true;
        logger.error("clusterSetSlotMigrating" + failedInfo(source, slot));
    }
    try {
        num = moveSlotData(source, target, slot, isPipelineMigrate);
    } catch (Exception e) {
        isError = true;
        logger.error(e.getMessage(), e);
    }
    if (!isError) {
        return num;
    } else {
        String errorMessage = "source=%s target=%s slot=%d num=%d reShard failed";
        throw new RuntimeException(String.format(errorMessage, getNodeKey(source), getNodeKey(target), slot, num));
    }
}
Also used : JedisException(redis.clients.jedis.exceptions.JedisException) IdempotentConfirmer(com.sohu.cache.util.IdempotentConfirmer) JedisException(redis.clients.jedis.exceptions.JedisException)

Example 13 with JedisException

use of redis.clients.jedis.exceptions.JedisException in project cachecloud by sohutv.

the class JedisPubSub method process.

private void process(Client client) {
    do {
        List<Object> reply = client.getRawObjectMultiBulkReply();
        final Object firstObj = reply.get(0);
        if (!(firstObj instanceof byte[])) {
            throw new JedisException("Unknown message type: " + firstObj);
        }
        final byte[] resp = (byte[]) firstObj;
        if (Arrays.equals(SUBSCRIBE.raw, resp)) {
            subscribedChannels = ((Long) reply.get(2)).intValue();
            final byte[] bchannel = (byte[]) reply.get(1);
            final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel);
            onSubscribe(strchannel, subscribedChannels);
        } else if (Arrays.equals(UNSUBSCRIBE.raw, resp)) {
            subscribedChannels = ((Long) reply.get(2)).intValue();
            final byte[] bchannel = (byte[]) reply.get(1);
            final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel);
            onUnsubscribe(strchannel, subscribedChannels);
        } else if (Arrays.equals(MESSAGE.raw, resp)) {
            final byte[] bchannel = (byte[]) reply.get(1);
            final byte[] bmesg = (byte[]) reply.get(2);
            final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel);
            final String strmesg = (bmesg == null) ? null : SafeEncoder.encode(bmesg);
            onMessage(strchannel, strmesg);
        } else if (Arrays.equals(PMESSAGE.raw, resp)) {
            final byte[] bpattern = (byte[]) reply.get(1);
            final byte[] bchannel = (byte[]) reply.get(2);
            final byte[] bmesg = (byte[]) reply.get(3);
            final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern);
            final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel);
            final String strmesg = (bmesg == null) ? null : SafeEncoder.encode(bmesg);
            onPMessage(strpattern, strchannel, strmesg);
        } else if (Arrays.equals(PSUBSCRIBE.raw, resp)) {
            subscribedChannels = ((Long) reply.get(2)).intValue();
            final byte[] bpattern = (byte[]) reply.get(1);
            final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern);
            onPSubscribe(strpattern, subscribedChannels);
        } else if (Arrays.equals(PUNSUBSCRIBE.raw, resp)) {
            subscribedChannels = ((Long) reply.get(2)).intValue();
            final byte[] bpattern = (byte[]) reply.get(1);
            final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern);
            onPUnsubscribe(strpattern, subscribedChannels);
        } else {
            throw new JedisException("Unknown message type: " + firstObj);
        }
    } while (isSubscribed());
    /* Invalidate instance since this thread is no longer listening */
    this.client = null;
}
Also used : JedisException(redis.clients.jedis.exceptions.JedisException)

Example 14 with JedisException

use of redis.clients.jedis.exceptions.JedisException in project jedis by xetorthio.

the class BinaryJedisPubSub method process.

private void process(Client client) {
    do {
        List<Object> reply = client.getRawObjectMultiBulkReply();
        final Object firstObj = reply.get(0);
        if (!(firstObj instanceof byte[])) {
            throw new JedisException("Unknown message type: " + firstObj);
        }
        final byte[] resp = (byte[]) firstObj;
        if (Arrays.equals(SUBSCRIBE.raw, resp)) {
            subscribedChannels = ((Long) reply.get(2)).intValue();
            final byte[] bchannel = (byte[]) reply.get(1);
            onSubscribe(bchannel, subscribedChannels);
        } else if (Arrays.equals(UNSUBSCRIBE.raw, resp)) {
            subscribedChannels = ((Long) reply.get(2)).intValue();
            final byte[] bchannel = (byte[]) reply.get(1);
            onUnsubscribe(bchannel, subscribedChannels);
        } else if (Arrays.equals(MESSAGE.raw, resp)) {
            final byte[] bchannel = (byte[]) reply.get(1);
            final byte[] bmesg = (byte[]) reply.get(2);
            onMessage(bchannel, bmesg);
        } else if (Arrays.equals(PMESSAGE.raw, resp)) {
            final byte[] bpattern = (byte[]) reply.get(1);
            final byte[] bchannel = (byte[]) reply.get(2);
            final byte[] bmesg = (byte[]) reply.get(3);
            onPMessage(bpattern, bchannel, bmesg);
        } else if (Arrays.equals(PSUBSCRIBE.raw, resp)) {
            subscribedChannels = ((Long) reply.get(2)).intValue();
            final byte[] bpattern = (byte[]) reply.get(1);
            onPSubscribe(bpattern, subscribedChannels);
        } else if (Arrays.equals(PUNSUBSCRIBE.raw, resp)) {
            subscribedChannels = ((Long) reply.get(2)).intValue();
            final byte[] bpattern = (byte[]) reply.get(1);
            onPUnsubscribe(bpattern, subscribedChannels);
        } else {
            throw new JedisException("Unknown message type: " + firstObj);
        }
    } while (isSubscribed());
}
Also used : JedisException(redis.clients.jedis.exceptions.JedisException)

Example 15 with JedisException

use of redis.clients.jedis.exceptions.JedisException in project jedis by xetorthio.

the class JedisPool method returnResource.

@Override
protected void returnResource(final Jedis resource) {
    if (resource != null) {
        try {
            resource.resetState();
            returnResourceObject(resource);
        } catch (Exception e) {
            returnBrokenResource(resource);
            throw new JedisException("Could not return the resource to the pool", e);
        }
    }
}
Also used : JedisException(redis.clients.jedis.exceptions.JedisException) JedisException(redis.clients.jedis.exceptions.JedisException)

Aggregations

JedisException (redis.clients.jedis.exceptions.JedisException)15 Jedis (redis.clients.jedis.Jedis)8 Transaction (redis.clients.jedis.Transaction)4 TicketModel (com.gitblit.models.TicketModel)3 Change (com.gitblit.models.TicketModel.Change)3 IdempotentConfirmer (com.sohu.cache.util.IdempotentConfirmer)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1 JedisPool (redis.clients.jedis.JedisPool)1