/** * Tests that offsets are properly committed to ZooKeeper and initial offsets are read from * ZooKeeper. * * <p>This test is only applicable if the Flink Kafka Consumer uses the ZooKeeperOffsetHandler. */ @Test(timeout = 60000) public void testOffsetInZookeeper() throws Exception { final int parallelism = 3; // write a sequence from 0 to 99 to each of the 3 partitions. final String topicName = writeSequence("testOffsetInZK", 100, parallelism, 1); StreamExecutionEnvironment env1 = StreamExecutionEnvironment.createRemoteEnvironment("localhost", flinkPort); env1.getConfig().disableSysoutLogging(); env1.enableCheckpointing(50); env1.getConfig().setRestartStrategy(RestartStrategies.noRestart()); env1.setParallelism(parallelism); StreamExecutionEnvironment env2 = StreamExecutionEnvironment.createRemoteEnvironment("localhost", flinkPort); env2.getConfig().disableSysoutLogging(); env2.enableCheckpointing(50); env2.getConfig().setRestartStrategy(RestartStrategies.noRestart()); env2.setParallelism(parallelism); readSequence(env1, standardProps, parallelism, topicName, 100, 0); CuratorFramework curatorClient = ((KafkaTestEnvironmentImpl) kafkaServer).createCuratorClient(); Long o1 = ZookeeperOffsetHandler.getOffsetFromZooKeeper( curatorClient, standardProps.getProperty("group.id"), topicName, 0); Long o2 = ZookeeperOffsetHandler.getOffsetFromZooKeeper( curatorClient, standardProps.getProperty("group.id"), topicName, 1); Long o3 = ZookeeperOffsetHandler.getOffsetFromZooKeeper( curatorClient, standardProps.getProperty("group.id"), topicName, 2); LOG.info("Got final offsets from zookeeper o1={}, o2={}, o3={}", o1, o2, o3); assertTrue(o1 == null || (o1 >= 0 && o1 <= 100)); assertTrue(o2 == null || (o2 >= 0 && o2 <= 100)); assertTrue(o3 == null || (o3 >= 0 && o3 <= 100)); LOG.info("Manipulating offsets"); // set the offset to 50 for the three partitions ZookeeperOffsetHandler.setOffsetInZooKeeper( curatorClient, standardProps.getProperty("group.id"), topicName, 0, 49); ZookeeperOffsetHandler.setOffsetInZooKeeper( curatorClient, standardProps.getProperty("group.id"), topicName, 1, 49); ZookeeperOffsetHandler.setOffsetInZooKeeper( curatorClient, standardProps.getProperty("group.id"), topicName, 2, 49); curatorClient.close(); // create new env readSequence(env2, standardProps, parallelism, topicName, 50, 50); deleteTestTopic(topicName); }
@Test(timeout = 60000) public void testInvalidOffset() throws Exception { final String topic = "invalidOffsetTopic"; final int parallelism = 1; // create topic createTestTopic(topic, parallelism, 1); final StreamExecutionEnvironment env = StreamExecutionEnvironment.createRemoteEnvironment("localhost", flinkPort); // write 20 messages into topic: writeSequence(env, topic, 20, parallelism); // set invalid offset: CuratorFramework curatorClient = ((KafkaTestEnvironmentImpl) kafkaServer).createCuratorClient(); ZookeeperOffsetHandler.setOffsetInZooKeeper( curatorClient, standardProps.getProperty("group.id"), topic, 0, 1234); curatorClient.close(); // read from topic final int valuesCount = 20; final int startFrom = 0; readSequence(env, standardProps, parallelism, topic, valuesCount, startFrom); deleteTestTopic(topic); }
@Test public void runOffsetManipulationInZooKeeperTest() { try { final String topicName = "ZookeeperOffsetHandlerTest-Topic"; final String groupId = "ZookeeperOffsetHandlerTest-Group"; final Long offset = (long) (Math.random() * Long.MAX_VALUE); CuratorFramework curatorFramework = ((KafkaTestEnvironmentImpl) kafkaServer).createCuratorClient(); kafkaServer.createTestTopic(topicName, 3, 2); ZookeeperOffsetHandler.setOffsetInZooKeeper(curatorFramework, groupId, topicName, 0, offset); Long fetchedOffset = ZookeeperOffsetHandler.getOffsetFromZooKeeper(curatorFramework, groupId, topicName, 0); curatorFramework.close(); assertEquals(offset, fetchedOffset); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
@Override public void commit(Map<KafkaTopicPartition, Long> offsetsToCommit) throws Exception { for (Map.Entry<KafkaTopicPartition, Long> entry : offsetsToCommit.entrySet()) { KafkaTopicPartition tp = entry.getKey(); long offset = entry.getValue(); if (offset >= 0) { setOffsetInZooKeeper(curatorClient, groupId, tp.getTopic(), tp.getPartition(), offset); } } }