Search in sources :

Example 1 with ConsumerApiResult

use of com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult in project LinkAgent by shulieTech.

the class ZkWithoutIpCacheSupport method renew.

/**
 * 注意这里,不完全保证一定能拿到最新的
 */
private void renew(Supplier supplier) {
    try {
        if (lock.acquire(10, TimeUnit.MILLISECONDS)) {
            try {
                List<ConsumerApiResult> newList = supplier.get();
                if (newList == null) {
                    throw new PradarException("supplier invoke but not data return!");
                }
                CACHE = group(newList);
                putInZK(newList);
            } finally {
                lock.release();
            }
        } else {
            // 如果没获取到锁,锁一定是被其它节点获取,这里等到能获取到锁的时候,一定是zk上的数据已经更新了
            lock.acquire();
            try {
                List<ConsumerApiResult> newList = getFromZK();
                if (newList == null) {
                    throw new PradarException("get lock but zk not update! this should never happened!");
                }
                CACHE = group(newList);
            } finally {
                lock.release();
            }
        }
    } catch (Exception e) {
        throw new PradarException(e);
    }
}
Also used : PradarException(com.pamirs.pradar.exception.PradarException) ConsumerApiResult(com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult) ZipException(java.util.zip.ZipException) KeeperException(org.apache.zookeeper.KeeperException) PradarException(com.pamirs.pradar.exception.PradarException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

Example 2 with ConsumerApiResult

use of com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult in project LinkAgent by shulieTech.

the class SimpleLocalCacheSupport method computeIfAbsent.

@Override
public ConsumerApiResult computeIfAbsent(CacheKey cacheKey, Supplier supplier) {
    if (cache == null) {
        synchronized (SimpleLocalCacheSupport.class) {
            if (cache == null) {
                renew(supplier);
            }
        }
    }
    ConsumerApiResult consumerApiResult = cache.get(cacheKey);
    if (consumerApiResult == null) {
        synchronized (SimpleLocalCacheSupport.class) {
            renew(supplier);
            consumerApiResult = cache.get(cacheKey);
        }
    }
    return consumerApiResult;
}
Also used : ConsumerApiResult(com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult)

Example 3 with ConsumerApiResult

use of com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult in project LinkAgent by shulieTech.

the class ZkWithIpCacheSupport method ipGroup.

private Map<String, List<ConsumerApiResult>> ipGroup(List<ConsumerApiResult> consumerApiResults) {
    Map<String, List<ConsumerApiResult>> result = new HashMap<String, List<ConsumerApiResult>>();
    for (ConsumerApiResult consumerApiResult : consumerApiResults) {
        String ip = ((WithCacheKey) cacheKeyBuilder.build(consumerApiResult)).getConnectionLocalIp();
        List<ConsumerApiResult> pieces = result.get(ip);
        if (pieces == null) {
            pieces = new ArrayList<ConsumerApiResult>();
            result.put(ip, pieces);
        }
        pieces.add(consumerApiResult);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ConsumerApiResult(com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with ConsumerApiResult

use of com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult in project LinkAgent by shulieTech.

the class ZkWithIpCacheSupport method renew.

/**
 * 注意这里,不完全保证一定能拿到最新的
 */
private void renew(Supplier supplier, String connectionLocalIp) {
    try {
        if (lock.acquire(10, TimeUnit.MILLISECONDS)) {
            try {
                List<ConsumerApiResult> newList = supplier.get();
                if (newList == null) {
                    throw new PradarException("supplier invoke but not data return!");
                }
                CACHE = group(newList);
                putInZK(newList);
            } finally {
                lock.release();
            }
        } else {
            // 如果没获取到锁,锁一定是被其它节点获取,这里等到能获取到锁的时候,一定是zk上的数据已经更新了
            lock.acquire();
            try {
                List<ConsumerApiResult> newList = getFromZK(connectionLocalIp);
                if (newList == null) {
                    throw new PradarException("get lock but zk not update! this should never happened!");
                }
                CACHE = group(newList);
            } finally {
                lock.release();
            }
        }
    } catch (Exception e) {
        throw new PradarException(e);
    }
}
Also used : PradarException(com.pamirs.pradar.exception.PradarException) ConsumerApiResult(com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult) ZipException(java.util.zip.ZipException) KeeperException(org.apache.zookeeper.KeeperException) PradarException(com.pamirs.pradar.exception.PradarException)

Example 5 with ConsumerApiResult

use of com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult in project LinkAgent by shulieTech.

the class ZkWithIpCacheSupport method computeIfAbsent.

@Override
public ConsumerApiResult computeIfAbsent(CacheKey key, Supplier supplier) {
    WithCacheKey cacheKey = (WithCacheKey) key;
    if (CACHE == null) {
        synchronized (ZkWithIpCacheSupport.class) {
            if (CACHE == null) {
                List<ConsumerApiResult> consumerApiResults = getFromZK(cacheKey.getConnectionLocalIp());
                if (consumerApiResults == null) {
                    renew(supplier, cacheKey.getConnectionLocalIp());
                    return CACHE.get(cacheKey);
                } else {
                    CACHE = group(consumerApiResults);
                    ConsumerApiResult consumerApiResult = CACHE.get(cacheKey);
                    if (consumerApiResult == null) {
                        renew(supplier, cacheKey.getConnectionLocalIp());
                        return CACHE.get(cacheKey);
                    } else {
                        return consumerApiResult;
                    }
                }
            }
        }
    }
    ConsumerApiResult consumerApiResult = CACHE.get(cacheKey);
    if (consumerApiResult == null) {
        synchronized (ZkWithIpCacheSupport.class) {
            renew(supplier, cacheKey.getConnectionLocalIp());
            consumerApiResult = CACHE.get(cacheKey);
        }
    }
    return consumerApiResult;
}
Also used : ConsumerApiResult(com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult)

Aggregations

ConsumerApiResult (com.pamirs.attach.plugin.rabbitmq.consumer.admin.support.ConsumerApiResult)6 PradarException (com.pamirs.pradar.exception.PradarException)2 ZipException (java.util.zip.ZipException)2 KeeperException (org.apache.zookeeper.KeeperException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)1