Search in sources :

Example 1 with Attachment

use of com.pamirs.attach.plugin.dynamic.Attachment in project LinkAgent by shulieTech.

the class DataSourceWrapUtil method attachment.

public static void attachment(Advice advice) {
    try {
        BasicDataSource target = (BasicDataSource) advice.getTarget();
        ResourceManager.set(new Attachment(target.getUrl(), "dbcp", Type.DataBaseType.types(), new DbcpTemplate().setUrl(target.getUrl()).setUsername(target.getUsername()).setPassword(target.getPassword()).setDriverClassName(target.getDriverClassName())));
    } catch (Throwable t) {
        logger.error(Throwables.getStackTraceAsString(t));
    }
}
Also used : Attachment(com.pamirs.attach.plugin.dynamic.Attachment) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) DbcpTemplate(com.pamirs.attach.plugin.dynamic.template.DbcpTemplate)

Example 2 with Attachment

use of com.pamirs.attach.plugin.dynamic.Attachment in project LinkAgent by shulieTech.

the class JedisClusterConnectionInterceptor method attachment.

void attachment(Advice advice) {
    try {
        if (Pradar.isClusterTest()) {
            return;
        }
        JedisSlotBasedConnectionHandler handler = (JedisSlotBasedConnectionHandler) advice.getTarget();
        JedisClusterInfoCache cache = Reflect.on(handler).get("cache");
        String password = Reflect.on(cache).get("password");
        Map hashmap = Reflect.on(cache).get("nodes");
        StringBuilder builder = new StringBuilder();
        for (Object node : hashmap.keySet()) {
            model.setClusterMode(String.valueOf(node));
            builder.append(node).append(",");
        }
        builder.deleteCharAt(builder.length() - 1);
        ResourceManager.set(new Attachment(new ArrayList(hashmap.keySet()), RedisConstants.PLUGIN_NAME, new String[] { RedisConstants.MIDDLEWARE_NAME }, new RedisTemplate.JedisClusterTemplate().setNodes(builder.toString()).setPassword(password)));
    } catch (Throwable t) {
    }
}
Also used : JedisSlotBasedConnectionHandler(redis.clients.jedis.JedisSlotBasedConnectionHandler) ArrayList(java.util.ArrayList) Attachment(com.pamirs.attach.plugin.dynamic.Attachment) JedisClusterInfoCache(redis.clients.jedis.JedisClusterInfoCache) Map(java.util.Map)

Example 3 with Attachment

use of com.pamirs.attach.plugin.dynamic.Attachment in project LinkAgent by shulieTech.

the class JedisInterceptor method attachment.

void attachment(Advice advice, int stage) {
    try {
        if (Pradar.isClusterTest()) {
            return;
        }
        Jedis jedis = (Jedis) advice.getTarget();
        Client client = Reflect.on(jedis).get("client");
        String node = client.getHost().concat(":").concat(String.valueOf(client.getPort()));
        Object dataSource = null;
        boolean isSentinel = false;
        try {
            /**
             * 哨兵模式的端口会经过处理,不是配置的,所以这里判断一下
             */
            dataSource = Reflect.on(jedis).get("dataSource");
            if (dataSource != null && JedisSentinelPool.class.isAssignableFrom(dataSource.getClass())) {
                isSentinel = true;
            }
            /**
             * 判断下拦截的方法,如果是slaveOf,说明是主从模式,和哨兵模式有一定区别
             */
            String method = advice.getBehavior().getName();
            /**
             * 这个实在难搞,加上对哨兵的过滤,因为哨兵也是基于主从做的
             */
            if ("slaveof".equals(method.toLowerCase()) && !isSentinel) {
                Object[] parameters = advice.getParameterArray();
                String master = parameters[0] + ":" + parameters[1];
                String slave = node;
                model.setMasterSlaveMode(slave, master);
            }
        } catch (Throwable t) {
        }
        switch(stage) {
            case 0:
                {
                    /**
                     * 主从模式
                     */
                    if (model.isMasterSlave(node)) {
                        String slave = node;
                        String master = model.getMasterBySlave(slave);
                        /*  if (master == null) {
                            break;
                        }
                        Attachment ext = new Attachment(
                                null, RedisConstants.PLUGIN_NAME, new String[]{RedisConstants.MIDDLEWARE_NAME},
                                new RedisTemplate.JedisMasterSlaveTemplate()
                                        .setMaster(master)
                                        .setNodes(slave));
                        Pradar.getInvokeContext().setExt(ext);*/
                        break;
                    } else if (model.isClusterMode(node)) {
                        /**
                         * 集群模式
                         */
                        Pradar.getInvokeContext().setIndex(node);
                        break;
                    } else if (isSentinel) {
                        /**
                         * 哨兵模式
                         */
                        JedisSentinelPool jedisSentinelPool = (JedisSentinelPool) dataSource;
                        String password = Reflect.on(jedisSentinelPool).get("password");
                        Integer database = Reflect.on(jedisSentinelPool).get("database");
                        Set set = Reflect.on(jedisSentinelPool).get("masterListeners");
                        Iterator iterator = set.iterator();
                        StringBuilder nodeBuilder = new StringBuilder();
                        String masterName = null;
                        while (iterator.hasNext()) {
                            Object t = iterator.next();
                            Reflect ref = Reflect.on(t);
                            masterName = ref.get("masterName");
                            String host = ref.get("host");
                            String port = String.valueOf(ref.get("port"));
                            nodeBuilder.append(host.concat(":").concat(port)).append(",");
                        }
                        Attachment ext = new Attachment(null, RedisConstants.PLUGIN_NAME, new String[] { RedisConstants.MIDDLEWARE_NAME }, new RedisTemplate.JedisSentinelTemplate().setMaster(masterName).setNodes(nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString()).setDatabase(database).setPassword(password));
                        Pradar.getInvokeContext().setExt(ext);
                    } else {
                        // 单机模式
                        String password = Reflect.on(client).get("password");
                        int db = Integer.parseInt(String.valueOf(Reflect.on(client).get("db")));
                        Attachment ext = new Attachment(node, RedisConstants.PLUGIN_NAME, new String[] { RedisConstants.MIDDLEWARE_NAME }, new RedisTemplate.JedisSingleTemplate().setNodes(node).setPassword(password).setDatabase(db));
                        Pradar.getInvokeContext().setExt(ext);
                        break;
                    }
                }
            case 1:
                return;
        }
    } catch (Throwable t) {
    }
}
Also used : Set(java.util.Set) RedisTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate) Attachment(com.pamirs.attach.plugin.dynamic.Attachment) Reflect(com.shulie.instrument.simulator.api.reflect.Reflect) Iterator(java.util.Iterator)

Example 4 with Attachment

use of com.pamirs.attach.plugin.dynamic.Attachment in project LinkAgent by shulieTech.

the class DataSourceGetConnectionArgsCutoffInterceptor method attachment.

void attachment(Advice advice) {
    try {
        ComboPooledDataSource target = (ComboPooledDataSource) advice.getTarget();
        ResourceManager.set(new Attachment(target.getJdbcUrl(), "c3p0", Type.DataBaseType.types(), new C3p0Template().setUrl(target.getJdbcUrl()).setUsername(target.getUser()).setPassword(target.getPassword()).setDriverClassName(target.getDriverClass()).setInitialPoolSize(target.getInitialPoolSize()).setMaxIdleTime(target.getMaxIdleTime()).setMaxPoolSize(target.getMaxPoolSize())));
    } catch (Throwable t) {
        logger.error(t.getMessage());
    }
}
Also used : ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) C3p0Template(com.pamirs.attach.plugin.dynamic.template.C3p0Template) Attachment(com.pamirs.attach.plugin.dynamic.Attachment)

Example 5 with Attachment

use of com.pamirs.attach.plugin.dynamic.Attachment in project LinkAgent by shulieTech.

the class DataSourceGetConnectionCutoffInterceptor method attachment.

void attachment(Advice advice) {
    try {
        ComboPooledDataSource target = (ComboPooledDataSource) advice.getTarget();
        ResourceManager.set(new Attachment(target.getJdbcUrl(), "c3p0", Type.DataBaseType.types(), new C3p0Template().setUrl(target.getJdbcUrl()).setUsername(target.getUser()).setPassword(target.getPassword()).setDriverClassName(target.getDriverClass()).setInitialPoolSize(target.getInitialPoolSize()).setMaxIdleTime(target.getMaxIdleTime()).setMaxPoolSize(target.getMaxPoolSize())));
    } catch (Throwable t) {
        logger.error(t.getMessage());
    }
}
Also used : ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) C3p0Template(com.pamirs.attach.plugin.dynamic.template.C3p0Template) Attachment(com.pamirs.attach.plugin.dynamic.Attachment)

Aggregations

Attachment (com.pamirs.attach.plugin.dynamic.Attachment)14 RedisTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate)4 ArrayList (java.util.ArrayList)3 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)2 C3p0Template (com.pamirs.attach.plugin.dynamic.template.C3p0Template)2 DbcpTemplate (com.pamirs.attach.plugin.dynamic.template.DbcpTemplate)2 Reflect (com.shulie.instrument.simulator.api.reflect.Reflect)2 RedisURI (io.lettuce.core.RedisURI)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Set (java.util.Set)2 AbstractTemplate (com.pamirs.attach.plugin.dynamic.template.AbstractTemplate)1 HbaseTemplate (com.pamirs.attach.plugin.dynamic.template.HbaseTemplate)1 HikariTemplate (com.pamirs.attach.plugin.dynamic.template.HikariTemplate)1 ProxoolTemplate (com.pamirs.attach.plugin.dynamic.template.ProxoolTemplate)1 RedissionSentinelTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSentinelTemplate)1 RedissionSingleTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSingleTemplate)1 RedissonMasterSlaveTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonMasterSlaveTemplate)1 RedissonReplicatedTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonReplicatedTemplate)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1