Search in sources :

Example 16 with CanalClientException

use of com.alibaba.otter.canal.protocol.exception.CanalClientException in project canal by alibaba.

the class SimpleCanalConnector method waitClientRunning.

private void waitClientRunning() {
    try {
        if (zkClientx != null) {
            if (!connected) {
                // 未调用connect
                throw new CanalClientException("should connect first");
            }
            running = true;
            // 阻塞等待
            mutex.get();
        } else {
            // 单机模式直接设置为running
            running = true;
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new CanalClientException(e);
    }
}
Also used : CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException)

Example 17 with CanalClientException

use of com.alibaba.otter.canal.protocol.exception.CanalClientException in project canal by alibaba.

the class SimpleCanalConnector method doConnect.

private InetSocketAddress doConnect() throws CanalClientException {
    try {
        channel = SocketChannel.open();
        channel.socket().setSoTimeout(soTimeout);
        SocketAddress address = getAddress();
        if (address == null) {
            address = getNextAddress();
        }
        channel.connect(address);
        readableChannel = Channels.newChannel(channel.socket().getInputStream());
        writableChannel = Channels.newChannel(channel.socket().getOutputStream());
        Packet p = Packet.parseFrom(readNextPacket());
        if (p.getVersion() != 1) {
            throw new CanalClientException("unsupported version at this client.");
        }
        if (p.getType() != PacketType.HANDSHAKE) {
            throw new CanalClientException("expect handshake but found other type.");
        }
        // 
        Handshake handshake = Handshake.parseFrom(p.getBody());
        supportedCompressions.add(handshake.getSupportedCompressions());
        // 
        // seed for auth
        ByteString seed = handshake.getSeeds();
        String newPasswd = password;
        if (password != null) {
            // encode passwd
            newPasswd = SecurityUtil.byte2HexStr(SecurityUtil.scramble411(password.getBytes(), seed.toByteArray()));
        }
        ClientAuth ca = ClientAuth.newBuilder().setUsername(username != null ? username : "").setPassword(ByteString.copyFromUtf8(newPasswd != null ? newPasswd : "")).setNetReadTimeout(idleTimeout).setNetWriteTimeout(idleTimeout).build();
        writeWithHeader(Packet.newBuilder().setType(PacketType.CLIENTAUTHENTICATION).setBody(ca.toByteString()).build().toByteArray());
        // 
        Packet ack = Packet.parseFrom(readNextPacket());
        if (ack.getType() != PacketType.ACK) {
            throw new CanalClientException("unexpected packet type when ack is expected");
        }
        Ack ackBody = Ack.parseFrom(ack.getBody());
        if (ackBody.getErrorCode() > 0) {
            throw new CanalClientException("something goes wrong when doing authentication: " + ackBody.getErrorMessage());
        }
        connected = true;
        return new InetSocketAddress(channel.socket().getLocalAddress(), channel.socket().getLocalPort());
    } catch (IOException | NoSuchAlgorithmException e) {
        throw new CanalClientException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.CanalPacket.Packet) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) ByteString(com.google.protobuf.ByteString) InetSocketAddress(java.net.InetSocketAddress) Ack(com.alibaba.otter.canal.protocol.CanalPacket.Ack) ClientAck(com.alibaba.otter.canal.protocol.CanalPacket.ClientAck) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ClientAuth(com.alibaba.otter.canal.protocol.CanalPacket.ClientAuth) Handshake(com.alibaba.otter.canal.protocol.CanalPacket.Handshake)

Example 18 with CanalClientException

use of com.alibaba.otter.canal.protocol.exception.CanalClientException in project canal by alibaba.

the class SimpleCanalConnector method rollback.

@Override
public void rollback(long batchId) throws CanalClientException {
    waitClientRunning();
    ClientRollback ca = ClientRollback.newBuilder().setDestination(clientIdentity.getDestination()).setClientId(String.valueOf(clientIdentity.getClientId())).setBatchId(batchId).build();
    try {
        writeWithHeader(Packet.newBuilder().setType(PacketType.CLIENTROLLBACK).setBody(ca.toByteString()).build().toByteArray());
    } catch (IOException e) {
        throw new CanalClientException(e);
    }
}
Also used : ClientRollback(com.alibaba.otter.canal.protocol.CanalPacket.ClientRollback) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) IOException(java.io.IOException)

Example 19 with CanalClientException

use of com.alibaba.otter.canal.protocol.exception.CanalClientException in project canal by alibaba.

the class ClientRunningMonitor method initRunning.

// 改动记录:
// 1,在方法上加synchronized关键字,保证同步顺序执行;
// 2,判断Zk上已经存在的activeData是否是本机,是的话把mutex重置为true,否则会导致死锁
// 3,增加异常处理,保证出现异常时,running节点能被删除,否则会导致死锁
public synchronized void initRunning() {
    if (!isStart()) {
        return;
    }
    String path = ZookeeperPathUtils.getDestinationClientRunning(this.destination, clientData.getClientId());
    // 序列化
    byte[] bytes = JsonUtils.marshalToByte(clientData);
    try {
        mutex.set(false);
        zkClient.create(path, bytes, CreateMode.EPHEMERAL);
        // 触发一下事件
        processActiveEnter();
        activeData = clientData;
        mutex.set(true);
    } catch (ZkNodeExistsException e) {
        bytes = zkClient.readData(path, true);
        if (bytes == null) {
            // 如果不存在节点,立即尝试一次
            initRunning();
        } else {
            activeData = JsonUtils.unmarshalFromByte(bytes, ClientRunningData.class);
            // 如果发现已经存在,判断一下是否自己,避免活锁
            if (activeData.getAddress().contains(":") && isMine(activeData.getAddress())) {
                mutex.set(true);
            }
        }
    } catch (ZkNoNodeException e) {
        zkClient.createPersistent(ZookeeperPathUtils.getClientIdNodePath(this.destination, clientData.getClientId()), // 尝试创建父节点
        true);
        initRunning();
    } catch (Throwable t) {
        logger.error(MessageFormat.format("There is an error when execute initRunning method, with destination [{0}].", destination), t);
        // fixed issue 1220, 针对server节点不工作避免死循环
        if (t instanceof ServerNotFoundException) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        // 出现任何异常尝试release
        releaseRunning();
        throw new CanalClientException("something goes wrong in initRunning method. ", t);
    }
}
Also used : ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) ServerNotFoundException(com.alibaba.otter.canal.client.impl.ServerNotFoundException) ZkInterruptedException(org.I0Itec.zkclient.exception.ZkInterruptedException)

Example 20 with CanalClientException

use of com.alibaba.otter.canal.protocol.exception.CanalClientException in project canal by alibaba.

the class ClusterCanalConnector method connect.

@Override
public void connect() throws CanalClientException {
    while (currentConnector == null) {
        int times = 0;
        while (true) {
            try {
                currentConnector = new SimpleCanalConnector(null, username, password, destination) {

                    @Override
                    public SocketAddress getNextAddress() {
                        return accessStrategy.nextNode();
                    }
                };
                currentConnector.setSoTimeout(soTimeout);
                currentConnector.setIdleTimeout(idleTimeout);
                if (filter != null) {
                    currentConnector.setFilter(filter);
                }
                if (accessStrategy instanceof ClusterNodeAccessStrategy) {
                    currentConnector.setZkClientx(((ClusterNodeAccessStrategy) accessStrategy).getZkClient());
                }
                currentConnector.connect();
                break;
            } catch (Exception e) {
                logger.warn("failed to connect to:{} after retry {} times", accessStrategy.currentNode(), times);
                currentConnector.disconnect();
                currentConnector = null;
                // retry for #retryTimes for each node when trying to
                // connect to it.
                times = times + 1;
                if (times >= retryTimes) {
                    throw new CanalClientException(e);
                } else {
                    // fixed issue #55,增加sleep控制,避免重试connect时cpu使用过高
                    try {
                        Thread.sleep(retryInterval);
                    } catch (InterruptedException e1) {
                        throw new CanalClientException(e1);
                    }
                }
            }
        }
    }
}
Also used : CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) SocketAddress(java.net.SocketAddress) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException)

Aggregations

CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)21 IOException (java.io.IOException)9 Message (com.alibaba.otter.canal.protocol.Message)6 ClientAck (com.alibaba.otter.canal.protocol.CanalPacket.ClientAck)4 Packet (com.alibaba.otter.canal.protocol.CanalPacket.Packet)4 Ack (com.alibaba.otter.canal.protocol.CanalPacket.Ack)3 ByteString (com.google.protobuf.ByteString)3 CommonMessage (com.alibaba.otter.canal.connector.core.consumer.CommonMessage)2 FlatMessage (com.alibaba.otter.canal.protocol.FlatMessage)2 SocketAddress (java.net.SocketAddress)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 TimeoutException (java.util.concurrent.TimeoutException)2 org.apache.pulsar.client.api (org.apache.pulsar.client.api)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 MessageExt (org.apache.rocketmq.common.message.MessageExt)2 ServiceException (com.alibaba.otter.canal.admin.common.exception.ServiceException)1 ConsumerBatchMessage (com.alibaba.otter.canal.client.ConsumerBatchMessage)1 ServerNotFoundException (com.alibaba.otter.canal.client.impl.ServerNotFoundException)1