Search in sources :

Example 1 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) throws CanalStoreException {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        long sequence = ackSequence.get();
        long maxSequence = getSequence.get();
        boolean hasMatch = false;
        long memsize = 0;
        for (long next = sequence + 1; next <= maxSequence; next++) {
            Event event = entries[getIndex(next)];
            memsize += calculateSize(event);
            boolean match = CanalEventUtils.checkPosition(event, (LogPosition) position);
            if (match) {
                // 找到对应的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;
                    }
                }
                if (ackSequence.compareAndSet(sequence, next)) {
                    // 避免并发ack
                    notFull.signal();
                    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)

Example 2 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 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 3 with CanalStoreException

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

the class MemoryEventStoreMemBatchTest method testBlockPutOneGet.

@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 e) {
    } catch (InterruptedException e) {
    }
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(new Runnable() {

        public void run() {
            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) Test(org.junit.Test)

Example 4 with CanalStoreException

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

the class MemoryEventStorePutAndGetTest method testBlockPutOneGet.

@Test
public void testBlockPutOneGet() {
    final MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    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 e) {
    } catch (InterruptedException e) {
    }
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(new Runnable() {

        public void run() {
            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) Test(org.junit.Test)

Example 5 with CanalStoreException

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

the class MemoryEventStorePutAndGetTest method testBlockPutOneGet.

@Test
public void testBlockPutOneGet() {
    final MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    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) Test(org.junit.Test)

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