Search in sources :

Example 6 with CanalStoreException

use of com.alibaba.otter.canal.store.CanalStoreException in project canal by alibaba.

the class MemoryEventStorePutAndGetTest method testFullPut.

@Test
public void testFullPut() {
    int bufferSize = 16;
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(bufferSize);
    eventStore.start();
    for (int i = 0; i < bufferSize; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + i));
        Assert.assertTrue(result);
    }
    boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + bufferSize));
    Assert.assertFalse(result);
    try {
        result = eventStore.put(buildEvent("1", 1L, 1L + bufferSize), 1000L, TimeUnit.MILLISECONDS);
    } catch (CanalStoreException e) {
        Assert.fail(e.getMessage());
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    Assert.assertFalse(result);
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) CanalStoreException(com.alibaba.otter.canal.store.CanalStoreException) Test(org.junit.Test)

Example 7 with CanalStoreException

use of com.alibaba.otter.canal.store.CanalStoreException in project canal by alibaba.

the class MemoryEventStoreMemBatchTest method testBlockPutOneGet.

@Ignore
@Test
public void testBlockPutOneGet() {
    final MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(16);
    eventStore.setBatchMode(BatchMode.MEMSIZE);
    eventStore.start();
    final int batchSize = 10;
    for (int i = 0; i < batchSize; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L));
        Assert.assertTrue(result);
    }
    final Position position = eventStore.getFirstPosition();
    try {
        Events<Event> entrys = eventStore.get(position, batchSize);
        Assert.assertTrue(entrys.getEvents().size() == batchSize);
        Assert.assertEquals(position, entrys.getPositionRange().getStart());
        Assert.assertEquals(position, entrys.getPositionRange().getEnd());
    } catch (CanalStoreException | InterruptedException e) {
    }
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(() -> {
        boolean result = false;
        try {
            eventStore.get(position, batchSize);
        } catch (CanalStoreException e) {
        } catch (InterruptedException e) {
            System.out.println("interrupt occured.");
            result = true;
        }
        Assert.assertTrue(result);
    });
    try {
        Thread.sleep(1000L);
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    executor.shutdownNow();
    try {
        Thread.sleep(1000L);
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) Position(com.alibaba.otter.canal.protocol.position.Position) ExecutorService(java.util.concurrent.ExecutorService) Event(com.alibaba.otter.canal.store.model.Event) CanalStoreException(com.alibaba.otter.canal.store.CanalStoreException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with CanalStoreException

use of com.alibaba.otter.canal.store.CanalStoreException in project canal by alibaba.

the class MemoryEventStoreMemBatchTest method testFullPut.

@Test
public void testFullPut() {
    int bufferSize = 16;
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(bufferSize);
    eventStore.setBatchMode(BatchMode.MEMSIZE);
    eventStore.start();
    for (int i = 0; i < bufferSize; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + i));
        Assert.assertTrue(result);
    }
    boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + bufferSize));
    Assert.assertFalse(result);
    try {
        result = eventStore.put(buildEvent("1", 1L, 1L + bufferSize), 1000L, TimeUnit.MILLISECONDS);
    } catch (CanalStoreException | InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    Assert.assertFalse(result);
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) CanalStoreException(com.alibaba.otter.canal.store.CanalStoreException) Test(org.junit.Test)

Example 9 with CanalStoreException

use of com.alibaba.otter.canal.store.CanalStoreException in project canal by alibaba.

the class MemoryEventStorePutAndGetTest method testFullPut.

@Test
public void testFullPut() {
    int bufferSize = 16;
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(bufferSize);
    eventStore.start();
    for (int i = 0; i < bufferSize; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + i));
        Assert.assertTrue(result);
    }
    boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + bufferSize));
    Assert.assertFalse(result);
    try {
        result = eventStore.put(buildEvent("1", 1L, 1L + bufferSize), 1000L, TimeUnit.MILLISECONDS);
    } catch (CanalStoreException | InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    Assert.assertFalse(result);
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) CanalStoreException(com.alibaba.otter.canal.store.CanalStoreException) Test(org.junit.Test)

Example 10 with CanalStoreException

use of com.alibaba.otter.canal.store.CanalStoreException in project canal by alibaba.

the class MemoryEventStoreWithBuffer method cleanUntil.

public void cleanUntil(Position position, Long seqId) throws CanalStoreException {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        long sequence = ackSequence.get();
        long maxSequence = getSequence.get();
        boolean hasMatch = false;
        long memsize = 0;
        // ack没有list,但有已存在的foreach,还是节省一下list的开销
        long localExecTime = 0L;
        int deltaRows = 0;
        if (seqId > 0) {
            maxSequence = seqId;
        }
        for (long next = sequence + 1; next <= maxSequence; next++) {
            Event event = entries[getIndex(next)];
            if (localExecTime == 0 && event.getExecuteTime() > 0) {
                localExecTime = event.getExecuteTime();
            }
            deltaRows += event.getRowsCount();
            memsize += calculateSize(event);
            if ((seqId < 0 || next == seqId) && CanalEventUtils.checkPosition(event, (LogPosition) position)) {
                // 找到对应的position,更新ack seq
                hasMatch = true;
                if (batchMode.isMemSize()) {
                    ackMemSize.addAndGet(memsize);
                    // 尝试清空buffer中的内存,将ack之前的内存全部释放掉
                    for (long index = sequence + 1; index < next; index++) {
                        // 设置为null
                        entries[getIndex(index)] = null;
                    }
                    // 考虑getFirstPosition/getLastPosition会获取最后一次ack的position信息
                    // ack清理的时候只处理entry=null,释放内存
                    Event lastEvent = entries[getIndex(next)];
                    lastEvent.setEntry(null);
                    lastEvent.setRawEntry(null);
                }
                if (ackSequence.compareAndSet(sequence, next)) {
                    // 避免并发ack
                    notFull.signal();
                    ackTableRows.addAndGet(deltaRows);
                    if (localExecTime > 0) {
                        ackExecTime.lazySet(localExecTime);
                    }
                    return;
                }
            }
        }
        if (!hasMatch) {
            // 找不到对应需要ack的position
            throw new CanalStoreException("no match ack position" + position.toString());
        }
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Event(com.alibaba.otter.canal.store.model.Event) CanalStoreException(com.alibaba.otter.canal.store.CanalStoreException) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition)

Aggregations

CanalStoreException (com.alibaba.otter.canal.store.CanalStoreException)10 MemoryEventStoreWithBuffer (com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer)8 Test (org.junit.Test)8 Event (com.alibaba.otter.canal.store.model.Event)6 Position (com.alibaba.otter.canal.protocol.position.Position)4 ExecutorService (java.util.concurrent.ExecutorService)4 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)1 Ignore (org.junit.Ignore)1