Search in sources :

Example 1 with JedisException

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

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 2 with JedisException

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

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)

Example 3 with JedisException

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

the class RedisTicketService method deleteTicketImpl.

/**
	 * Deletes a ticket.
	 *
	 * @param ticket
	 * @return true if successful
	 */
@Override
protected boolean deleteTicketImpl(RepositoryModel repository, TicketModel ticket, String deletedBy) {
    boolean success = false;
    if (ticket == null) {
        throw new RuntimeException("must specify a ticket!");
    }
    Jedis jedis = pool.getResource();
    if (jedis == null) {
        return false;
    }
    try {
        // atomically remove ticket
        Transaction t = jedis.multi();
        t.del(key(repository, KeyType.ticket, ticket.number));
        t.del(key(repository, KeyType.journal, ticket.number));
        t.exec();
        success = true;
        log.debug("deleted ticket {} from Redis @ {}", "" + ticket.number, getUrl());
    } catch (JedisException e) {
        log.error("failed to delete ticket from Redis @ " + getUrl(), e);
        pool.returnBrokenResource(jedis);
        jedis = null;
    } finally {
        if (jedis != null) {
            pool.returnResource(jedis);
        }
    }
    return success;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisException(redis.clients.jedis.exceptions.JedisException) Transaction(redis.clients.jedis.Transaction)

Example 4 with JedisException

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

the class RedisTicketService method commitChangeImpl.

/**
	 * Commit a ticket change to the repository.
	 *
	 * @param repository
	 * @param ticketId
	 * @param change
	 * @return true, if the change was committed
	 */
@Override
protected boolean commitChangeImpl(RepositoryModel repository, long ticketId, Change change) {
    Jedis jedis = pool.getResource();
    if (jedis == null) {
        return false;
    }
    try {
        List<Change> changes = getJournal(jedis, repository, ticketId);
        changes.add(change);
        // build a new effective ticket from the changes
        TicketModel ticket = TicketModel.buildTicket(changes);
        String object = TicketSerializer.serialize(ticket);
        String journal = TicketSerializer.serialize(change);
        // atomically store ticket
        Transaction t = jedis.multi();
        t.set(key(repository, KeyType.ticket, ticketId), object);
        t.rpush(key(repository, KeyType.journal, ticketId), journal);
        t.exec();
        log.debug("updated ticket {} in Redis @ {}", "" + ticketId, getUrl());
        return true;
    } catch (JedisException e) {
        log.error("failed to update ticket cache in Redis @ " + getUrl(), e);
        pool.returnBrokenResource(jedis);
        jedis = null;
    } finally {
        if (jedis != null) {
            pool.returnResource(jedis);
        }
    }
    return false;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisException(redis.clients.jedis.exceptions.JedisException) Transaction(redis.clients.jedis.Transaction) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change)

Example 5 with JedisException

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

the class RedisTicketService method getTickets.

/**
	 * Returns all the tickets in the repository. Querying tickets from the
	 * repository requires deserializing all tickets. This is an  expensive
	 * process and not recommended. Tickets should be indexed by Lucene and
	 * queries should be executed against that index.
	 *
	 * @param repository
	 * @param filter
	 *            optional filter to only return matching results
	 * @return a list of tickets
	 */
@Override
public List<TicketModel> getTickets(RepositoryModel repository, TicketFilter filter) {
    Jedis jedis = pool.getResource();
    List<TicketModel> list = new ArrayList<TicketModel>();
    if (jedis == null) {
        return list;
    }
    try {
        // Deserialize each journal, build the ticket, and optionally filter
        Set<String> keys = jedis.keys(key(repository, KeyType.journal, "*"));
        for (String key : keys) {
            // {repo}:journal:{id}
            String id = key.split(":")[2];
            long ticketId = Long.parseLong(id);
            List<Change> changes = getJournal(jedis, repository, ticketId);
            if (ArrayUtils.isEmpty(changes)) {
                log.warn("Empty journal for {}:{}", repository, ticketId);
                continue;
            }
            TicketModel ticket = TicketModel.buildTicket(changes);
            ticket.project = repository.projectPath;
            ticket.repository = repository.name;
            ticket.number = ticketId;
            // add the ticket, conditionally, to the list
            if (filter == null) {
                list.add(ticket);
            } else {
                if (filter.accept(ticket)) {
                    list.add(ticket);
                }
            }
        }
        // sort the tickets by creation
        Collections.sort(list);
    } catch (JedisException e) {
        log.error("failed to retrieve tickets from Redis @ " + getUrl(), e);
        pool.returnBrokenResource(jedis);
        jedis = null;
    } finally {
        if (jedis != null) {
            pool.returnResource(jedis);
        }
    }
    return list;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisException(redis.clients.jedis.exceptions.JedisException) ArrayList(java.util.ArrayList) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change)

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