Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Error State Issue when Kafka Cluster is Offline #19

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions kafka-transport/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>[1.2.9,)</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 1995-2016 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373
email: [email protected]
*/

package com.esri.geoevent.transport.kafka;

import org.apache.kafka.clients.ApiVersions;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.DescribeClusterOptions;
import org.apache.kafka.clients.admin.DescribeClusterResult;
import org.apache.kafka.common.Node;
import org.apache.kafka.common.protocol.ApiKeys;

import java.util.*;

public class GEKafkaAdminUtil
{

public static void performAdminClientValidation(Properties configProperties)
{
AdminClient adminClient = AdminClient.create(configProperties);
DescribeClusterOptions describeOptions = new DescribeClusterOptions();
describeOptions.timeoutMs(Integer.valueOf(10));
Timer timer = new Timer("ReconnectionTimer");
GEKafkaTimerTask timerTask = new GEKafkaTimerTask(adminClient, timer);
timer.scheduleAtFixedRate(timerTask, 0L, 10000L);
}

public static boolean checkAPILevelCompatibility(Node... kafkaNodes)
{
ApiVersions apiVersions = new ApiVersions();
ArrayList<Short> apiVersionCollection = new ArrayList<>();
ApiKeys apiKeys = ApiKeys.forId(0);
Arrays.stream(kafkaNodes).forEach(eachNode -> apiVersionCollection.add(apiVersions.get(eachNode.idString()).latestUsableVersion(apiKeys)));
long count = apiVersionCollection.stream().filter(predicate -> predicate.intValue() > ((short) 0.11)).count();
return count > 0L ? true : false;
}

public static synchronized Collection<Node> listAllNodesInCluster(Properties configProperties)
{
AdminClient adminClient = AdminClient.create(configProperties);
DescribeClusterOptions describeOptions = new DescribeClusterOptions();
describeOptions.timeoutMs(Integer.valueOf(10));
DescribeClusterResult describedCluster = adminClient.describeCluster(describeOptions);
ArrayList<Node> nodeArrayList = new ArrayList<>();
describedCluster.nodes().whenComplete((nodes, throwable) -> nodeArrayList.addAll(nodes));
return nodeArrayList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 1995-2016 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373
email: [email protected]
*/


package com.esri.geoevent.transport.kafka;

import com.esri.ges.framework.i18n.BundleLogger;
import com.esri.ges.framework.i18n.BundleLoggerFactory;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.DescribeClusterResult;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.Node;

import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutionException;

public class GEKafkaTimerTask extends TimerTask
{

private static final BundleLogger LOGGER = BundleLoggerFactory.getLogger(GEKafkaTimerTask.class);
private AdminClient adminClient;
private Timer timer;

public GEKafkaTimerTask(AdminClient adminClient, Timer timer)
{
this.adminClient = adminClient;
this.timer = timer;
}

@Override
public void run()
{
DescribeClusterResult clusterDescription = adminClient.describeCluster();
KafkaFuture<Collection<Node>> clusterNodes = clusterDescription.nodes();
try
{

long count = clusterNodes.get().stream().count();
if (count > 0)
{
cancel();
timer.cancel();
timer.purge();
}
}
catch (InterruptedException | ExecutionException intEx)
{
LOGGER.error("PROBLEM_WITH_CLUSTER", intEx);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 1995-2016 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373
email: [email protected]
*/

package com.esri.geoevent.transport.kafka;

import com.esri.ges.framework.i18n.BundleLogger;
import com.esri.ges.framework.i18n.BundleLoggerFactory;
import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;

import java.util.Collection;

public class GeoEventKafkaConsumerRebalancer implements ConsumerRebalanceListener
{

private boolean isFromBeginning;
private KafkaConsumer<byte[], byte[]> consumer;
private BundleLogger LOGGER = BundleLoggerFactory.getLogger(GeoEventKafkaConsumerRebalancer.class);


public GeoEventKafkaConsumerRebalancer(boolean fromBeginning, KafkaConsumer<byte[], byte[]> kafkaConsumer)
{
isFromBeginning = fromBeginning;
consumer = kafkaConsumer;
}


@Override
public void onPartitionsRevoked(Collection<TopicPartition> partitions)
{
partitions.forEach(topicPartition -> LOGGER.info("PARTITION_REVOKED_ON_TOPIC: " + topicPartition.topic()));
}

@Override
public void onPartitionsAssigned(Collection<TopicPartition> partitions)
{
if (isFromBeginning)
consumer.seekToBeginning(partitions);
partitions.forEach(topicPartition -> LOGGER.info("PARTITION_ASSIGNED_ON_TOPIC: " + topicPartition.topic()));
}

}

This file was deleted.

Loading