Search in sources :

Example 1 with BinlogFile

use of com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile in project canal by alibaba.

the class RdsLocalBinlogEventParser method start.

public void start() throws CanalParseException {
    try {
        Assert.notNull(accesskey);
        Assert.notNull(secretkey);
        Assert.notNull(instanceId);
        Assert.notNull(url);
        Assert.notNull(directory);
        if (endTime == null) {
            endTime = System.currentTimeMillis();
        }
        EntryPosition entryPosition = findStartPosition(null);
        if (entryPosition == null) {
            throw new PositionNotFoundException("position not found!");
        }
        Long startTimeInMill = entryPosition.getTimestamp();
        if (startTimeInMill == null || startTimeInMill <= 0) {
            throw new PositionNotFoundException("position timestamp is empty!");
        }
        startTime = startTimeInMill;
        List<BinlogFile> binlogFiles = RdsBinlogOpenApi.listBinlogFiles(url, accesskey, secretkey, instanceId, new Date(startTime), new Date(endTime));
        if (binlogFiles.isEmpty()) {
            throw new CanalParseException("start timestamp : " + startTimeInMill + " binlog files is empty");
        }
        binlogDownloadQueue = new BinlogDownloadQueue(binlogFiles, batchFileSize, directory);
        binlogDownloadQueue.silenceDownload();
        needWait = true;
        // try to download one file,use to test server id
        binlogDownloadQueue.tryOne();
    } catch (Throwable e) {
        logger.error("download binlog failed", e);
        throw new CanalParseException(e);
    }
    setParserExceptionHandler(this::handleMysqlParserException);
    super.start();
}
Also used : BinlogFile(com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile) PositionNotFoundException(com.alibaba.otter.canal.parse.exception.PositionNotFoundException) EntryPosition(com.alibaba.otter.canal.protocol.position.EntryPosition) Date(java.util.Date) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException)

Example 2 with BinlogFile

use of com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile in project canal by alibaba.

the class BinlogDownloadQueue method tryOne.

public BinlogFile tryOne() throws Throwable {
    BinlogFile binlogFile = binlogList.poll();
    if (binlogFile == null) {
        throw new CanalParseException("download binlog is null");
    }
    download(binlogFile);
    hostId = binlogFile.getHostInstanceID();
    this.currentSize++;
    return binlogFile;
}
Also used : BinlogFile(com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException)

Example 3 with BinlogFile

use of com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile in project canal by alibaba.

the class BinlogDownloadQueue method prepare.

public void prepare() throws InterruptedException {
    for (int i = this.currentSize; i < batchFileSize && !binlogList.isEmpty(); i++) {
        BinlogFile binlogFile = null;
        while (!binlogList.isEmpty()) {
            binlogFile = binlogList.poll();
            if (!binlogFile.getHostInstanceID().equalsIgnoreCase(hostId)) {
                continue;
            }
            break;
        }
        if (binlogFile == null) {
            break;
        }
        this.downloadQueue.put(binlogFile);
        this.lastDownload = "mysql-bin." + binlogFile.getFileName();
        this.currentSize++;
    }
}
Also used : BinlogFile(com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile)

Example 4 with BinlogFile

use of com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile in project canal by alibaba.

the class BinlogDownloadQueue method download.

private void download(BinlogFile binlogFile) throws Throwable {
    String downloadLink = binlogFile.getDownloadLink();
    String fileName = binlogFile.getFileName();
    downloadLink = downloadLink.trim();
    CloseableHttpClient httpClient = null;
    if (downloadLink.startsWith("https")) {
        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setMaxConnPerRoute(50);
        builder.setMaxConnTotal(100);
        // 创建支持忽略证书的https
        final SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> true).build();
        httpClient = HttpClientBuilder.create().setSSLContext(sslContext).setConnectionManager(new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)).build())).build();
    } else {
        httpClient = HttpClientBuilder.create().setMaxConnPerRoute(50).setMaxConnTotal(100).build();
    }
    HttpGet httpGet = new HttpGet(downloadLink);
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(TIMEOUT).setConnectionRequestTimeout(TIMEOUT).setSocketTimeout(TIMEOUT).build();
    httpGet.setConfig(requestConfig);
    HttpResponse response = httpClient.execute(httpGet);
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpResponseStatus.OK.code()) {
        throw new RuntimeException("download failed , url:" + downloadLink + " , statusCode:" + statusCode);
    }
    saveFile(new File(destDir), "mysql-bin." + fileName, response);
}
Also used : SSLContext(javax.net.ssl.SSLContext) StringUtils(org.apache.commons.lang.StringUtils) RegistryBuilder(org.apache.http.config.RegistryBuilder) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) LoggerFactory(org.slf4j.LoggerFactory) BinlogFile(com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile) RequestConfig(org.apache.http.client.config.RequestConfig) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) LinkedList(java.util.LinkedList) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) FileOutputStream(java.io.FileOutputStream) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) LockSupport(java.util.concurrent.locks.LockSupport) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) HttpGet(org.apache.http.client.methods.HttpGet) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Comparator(java.util.Comparator) InputStream(java.io.InputStream) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) BinlogFile(com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile) File(java.io.File)

Example 5 with BinlogFile

use of com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile in project canal by alibaba.

the class BinlogDownloadQueue method prepareBinlogList.

private void prepareBinlogList() {
    for (BinlogFile binlog : this.binlogList) {
        String fileName = StringUtils.substringBetween(binlog.getDownloadLink(), "mysql-bin.", "?");
        binlog.setFileName(fileName);
    }
    this.binlogList.sort(Comparator.comparing(BinlogFile::getFileName));
}
Also used : BinlogFile(com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile)

Aggregations

BinlogFile (com.alibaba.otter.canal.parse.inbound.mysql.rds.data.BinlogFile)6 CanalParseException (com.alibaba.otter.canal.parse.exception.CanalParseException)3 Date (java.util.Date)2 PositionNotFoundException (com.alibaba.otter.canal.parse.exception.PositionNotFoundException)1 RdsBackupPolicy (com.alibaba.otter.canal.parse.inbound.mysql.rds.data.RdsBackupPolicy)1 EntryPosition (com.alibaba.otter.canal.protocol.position.EntryPosition)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Comparator (java.util.Comparator)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 LockSupport (java.util.concurrent.locks.LockSupport)1 SSLContext (javax.net.ssl.SSLContext)1