Search in sources :

Example 1 with Reflect

use of com.shulie.instrument.simulator.api.reflect.Reflect 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 2 with Reflect

use of com.shulie.instrument.simulator.api.reflect.Reflect in project LinkAgent by shulieTech.

the class JedisNodesStrategy method match.

/**
 * obj是redis.clients.jedis.Jedis或者redis.clients.jedis.BinaryJedis
 *
 * @param obj 任意对象
 * @return
 */
@Override
public List<String> match(Object obj) {
    if (!BinaryJedis.class.isAssignableFrom(obj.getClass())) {
        LOGGER.info("not support type of jedis single mode , class is not" + " redis.clients.jedis.Jedis or redis.clients.jedis.BinaryJedis");
        return new ArrayList<String>();
    }
    List<String> nodes = new ArrayList<String>();
    try {
        Reflect reflect = Reflect.on(obj);
        Client client = reflect.get("client");
        Reflect clientRef = Reflect.on(client);
        String password = clientRef.get("password");
        String host = clientRef.get("host");
        String port = String.valueOf(clientRef.get("port"));
        String db = String.valueOf(clientRef.get("db"));
        nodes.add(getKey(host, port));
    } catch (Throwable e) {
        LOGGER.error("", e);
    }
    return nodes;
}
Also used : BinaryJedis(redis.clients.jedis.BinaryJedis) Reflect(com.shulie.instrument.simulator.api.reflect.Reflect) ArrayList(java.util.ArrayList) Client(redis.clients.jedis.Client)

Example 3 with Reflect

use of com.shulie.instrument.simulator.api.reflect.Reflect in project LinkAgent by shulieTech.

the class DubboConsumerInterceptor method beforeLast.

@Override
public void beforeLast(Advice advice) throws ProcessControlException {
    final RpcInvocation invocation = (RpcInvocation) advice.getParameterArray()[0];
    String interfaceName = getInterfaceName(invocation);
    String methodName = invocation.getMethodName();
    MatchConfig config = ClusterTestUtils.rpcClusterTest(interfaceName, methodName);
    invocation.setAttachment(PradarService.PRADAR_WHITE_LIST_CHECK, String.valueOf(config.isSuccess()));
    config.addArgs("args", advice.getParameterArray());
    config.addArgs("url", interfaceName.concat("#").concat(methodName));
    config.addArgs("isInterface", Boolean.TRUE);
    config.addArgs("class", interfaceName);
    config.addArgs("method", methodName);
    config.addArgs("invocation", invocation);
    if (isShentongEvent(interfaceName)) {
        config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, "true");
    }
    if (config.getStrategy() instanceof JsonMockStrategy) {
        fixJsonStrategy.processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config);
    }
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionCall() {

        @Override
        public Object call(Object param) {
            try {
                // for 2.8.4
                return Reflect.on("org.apache.dubbo.rpc.RpcResult").create(param).get();
            } catch (Exception e) {
                if (logger.isInfoEnabled()) {
                    logger.info("find dubbo 2.8.4 class org.apache.dubbo.rpc.RpcResult fail, find others!", e);
                }
            // 
            }
            try {
                Reflect reflect = Reflect.on("org.apache.dubbo.rpc.AsyncRpcResult");
                try {
                    // for 2.7.5
                    java.util.concurrent.CompletableFuture<AppResponse> future = new java.util.concurrent.CompletableFuture<AppResponse>();
                    future.complete(new AppResponse(param));
                    Reflect result = reflect.create(future, invocation);
                    return result.get();
                } catch (ReflectException e) {
                    // for 2.7.3
                    Reflect result = reflect.create(invocation);
                    return result.get();
                }
            } catch (Exception e) {
                logger.error("fail to load dubbo 2.7.x class org.apache.dubbo.rpc.AsyncRpcResult", e);
                throw new ReflectException("fail to load dubbo 2.7.x class org.apache.dubbo.rpc.AsyncRpcResult", e);
            }
        }
    });
}
Also used : MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) ReflectException(com.shulie.instrument.simulator.api.reflect.ReflectException) PradarException(com.pamirs.pradar.exception.PradarException) ReflectException(com.shulie.instrument.simulator.api.reflect.ReflectException) ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy) Reflect(com.shulie.instrument.simulator.api.reflect.Reflect) ExecutionCall(com.pamirs.pradar.internal.config.ExecutionCall)

Example 4 with Reflect

use of com.shulie.instrument.simulator.api.reflect.Reflect in project LinkAgent by shulieTech.

the class BaseRedissonTimeSeriesMethodInterceptor method attachment.

/**
 * 入参是org.redisson.config.BaseConfig
 * 但是有的版本是私有类
 *
 * @param config
 */
private void attachment(Object config) {
    try {
        if (Pradar.isClusterTest() || config == null) {
            return;
        }
        ClusterServersConfig config1 = null;
        Reflect reflect = Reflect.on(config);
        String password = null;
        try {
            password = reflect.get("password");
        } catch (Throwable t) {
        }
        Integer database = null;
        try {
            database = Integer.parseInt(String.valueOf(reflect.get("database")));
        } catch (Throwable t) {
        }
        AbstractTemplate template;
        if (config instanceof ClusterServersConfig) {
            List nodeAddresses = reflect.get("nodeAddresses");
            StringBuilder nodeBuilder = new StringBuilder();
            for (Object object : nodeAddresses) {
                if (URI.class.isAssignableFrom(object.getClass())) {
                    URI uri = (URI) object;
                    nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
                } else if (String.class.isAssignableFrom(object.getClass())) {
                    nodeBuilder.append(removePre(object.toString())).append(",");
                }
            }
            String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
            template = new RedisTemplate.RedissionClusterTemplate().setNodes(nodes).setPassword(password);
        } else if (config instanceof MasterSlaveServersConfig) {
            Set nodeAddresses = reflect.get("slaveAddresses");
            StringBuilder nodeBuilder = new StringBuilder();
            for (Object object : nodeAddresses) {
                if (URI.class.isAssignableFrom(object.getClass())) {
                    URI uri = (URI) object;
                    nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
                } else if (String.class.isAssignableFrom(object.getClass())) {
                    nodeBuilder.append(removePre(object.toString())).append(",");
                }
            }
            String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
            Object masterNodeObj = reflect.get("masterAddress");
            String masterAddr = "";
            if (masterNodeObj instanceof URI) {
                masterAddr = ((URI) masterNodeObj).getHost().concat(":").concat(String.valueOf(((URI) masterNodeObj).getPort()));
                ;
            } else {
                masterAddr = removePre(String.valueOf(masterNodeObj));
            }
            template = new RedissonMasterSlaveTemplate().setMaster(masterAddr).setNodes(nodes).setPassword(password).setDatabase(database);
        } else if (config instanceof SingleServerConfig) {
            String nodes = "";
            Object address = Reflect.on(config).get("address");
            if (String.class.isAssignableFrom(address.getClass())) {
                nodes = removePre((String) address);
            } else if (URI.class.isAssignableFrom(address.getClass())) {
                URI uri = (URI) address;
                nodes = uri.getHost().concat(":").concat(String.valueOf(uri.getPort()));
            }
            template = new RedissionSingleTemplate().setNodes(nodes).setPassword(password).setDatabase(database);
        } else if (config instanceof SentinelServersConfig) {
            List sentinelAddresses = reflect.get("sentinelAddresses");
            StringBuilder nodeBuilder = new StringBuilder();
            for (Object object : sentinelAddresses) {
                if (URI.class.isAssignableFrom(object.getClass())) {
                    URI uri = (URI) object;
                    nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
                } else if (String.class.isAssignableFrom(object.getClass())) {
                    nodeBuilder.append(removePre(object.toString())).append(",");
                }
            }
            String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
            template = new RedissionSentinelTemplate().setNodes(nodes).setMaster(removePre(((SentinelServersConfig) config).getMasterName())).setNodes(nodes).setDatabase((database));
        } else if (config instanceof ReplicatedServersConfig) {
            List nodeAddresses = reflect.get("nodeAddresses");
            StringBuilder nodeBuilder = new StringBuilder();
            for (Object object : nodeAddresses) {
                if (URI.class.isAssignableFrom(object.getClass())) {
                    URI uri = (URI) object;
                    nodeBuilder.append(uri.getHost().concat(":").concat(String.valueOf(uri.getPort()))).append(",");
                } else if (String.class.isAssignableFrom(object.getClass())) {
                    nodeBuilder.append(removePre(object.toString())).append(",");
                }
            }
            String nodes = nodeBuilder.deleteCharAt(nodeBuilder.length() - 1).toString();
            template = new RedissonReplicatedTemplate().setDatabase(database).setNodes(nodes).setPassword(password);
        } else {
            LOGGER.error("Redisson not instanceof any know config:{}", config);
            return;
        }
        final Attachment attachment = new Attachment(null, RedissonConstants.PLUGIN_NAME, new String[] { RedissonConstants.MIDDLEWARE_NAME }, template);
        // ResourceManager.set(attachment);
        Pradar.getInvokeContext().setExt(attachment);
    } catch (Throwable t) {
        LOGGER.error("Redisson attachment error", t);
    }
}
Also used : RedisTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate) Set(java.util.Set) RedissonReplicatedTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonReplicatedTemplate) ClusterServersConfig(org.redisson.config.ClusterServersConfig) SingleServerConfig(org.redisson.config.SingleServerConfig) RedissionSingleTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSingleTemplate) Attachment(com.pamirs.attach.plugin.dynamic.Attachment) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig) RedissonMasterSlaveTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissonMasterSlaveTemplate) URI(java.net.URI) ReplicatedServersConfig(org.redisson.config.ReplicatedServersConfig) Reflect(com.shulie.instrument.simulator.api.reflect.Reflect) AbstractTemplate(com.pamirs.attach.plugin.dynamic.template.AbstractTemplate) SentinelServersConfig(org.redisson.config.SentinelServersConfig) List(java.util.List) RedissionSentinelTemplate(com.pamirs.attach.plugin.dynamic.template.RedisTemplate.RedissionSentinelTemplate)

Example 5 with Reflect

use of com.shulie.instrument.simulator.api.reflect.Reflect in project LinkAgent by shulieTech.

the class JedisClusterFactory method createMediator.

@Override
public RedisClientMediator<JedisSlotBasedConnectionHandler> createMediator(Object connection, ShadowRedisConfig shadowConfig) {
    model.cachePressureNode(shadowConfig);
    RedisClientMediator<JedisSlotBasedConnectionHandler> mediator = null;
    if (connection instanceof JedisSlotBasedConnectionHandler) {
        Reflect reflect = Reflect.on(Reflect.on(connection).get("cache"));
        GenericObjectPoolConfig poolConfig = reflect.get("poolConfig");
        int connectionTimeout = reflect.get("connectionTimeout");
        int soTimeout = reflect.get("soTimeout");
        JedisSlotBasedConnectionHandler pressureJedisPool = null;
        String shadowPassword = shadowConfig.getPassword();
        if (!StringUtil.isEmpty(shadowPassword)) {
            pressureJedisPool = new JedisSlotBasedConnectionHandler(convert(shadowConfig.getNodeNums()), poolConfig, connectionTimeout, soTimeout, shadowConfig.getPassword());
        } else {
            pressureJedisPool = new JedisSlotBasedConnectionHandler(convert(shadowConfig.getNodeNums()), poolConfig, connectionTimeout, soTimeout);
        }
        mediator = new RedisClientMediator<JedisSlotBasedConnectionHandler>(pressureJedisPool, (JedisSlotBasedConnectionHandler) connection, true);
    }
    return mediator;
}
Also used : JedisSlotBasedConnectionHandler(redis.clients.jedis.JedisSlotBasedConnectionHandler) Reflect(com.shulie.instrument.simulator.api.reflect.Reflect) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig)

Aggregations

Reflect (com.shulie.instrument.simulator.api.reflect.Reflect)7 Attachment (com.pamirs.attach.plugin.dynamic.Attachment)2 RedisTemplate (com.pamirs.attach.plugin.dynamic.template.RedisTemplate)2 Set (java.util.Set)2 BinaryJedis (redis.clients.jedis.BinaryJedis)2 Client (redis.clients.jedis.Client)2 AbstractTemplate (com.pamirs.attach.plugin.dynamic.template.AbstractTemplate)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 PradarException (com.pamirs.pradar.exception.PradarException)1 ExecutionCall (com.pamirs.pradar.internal.config.ExecutionCall)1 MatchConfig (com.pamirs.pradar.internal.config.MatchConfig)1 JsonMockStrategy (com.pamirs.pradar.pressurement.mock.JsonMockStrategy)1 ProcessControlException (com.shulie.instrument.simulator.api.ProcessControlException)1 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1