Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.kafka.tools;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import joptsimple.OptionSpecBuilder;
import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.DeleteTopicsResult;
import org.apache.kafka.clients.admin.DescribeConsumerGroupsOptions;
import org.apache.kafka.clients.admin.DescribeConsumerGroupsResult;
import org.apache.kafka.clients.admin.MemberDescription;
import org.apache.kafka.clients.admin.RemoveMembersFromConsumerGroupOptions;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndTimestamp;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.annotation.InterfaceStability;
import org.apache.kafka.common.requests.ListOffsetsResponse;
import org.apache.kafka.common.serialization.ByteArrayDeserializer;
import org.apache.kafka.common.utils.Exit;
import org.apache.kafka.common.utils.Utils;
import org.apache.kafka.server.util.CommandLineUtils;
import java.io.IOException;
import java.text.ParseException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* {@link StreamsResetter} resets the processing state of a Kafka Streams application so that, for example,
* you can reprocess its input from scratch.
*
* This class is not part of public API. For backward compatibility,
* use the provided script in "bin/" instead of calling this class directly from your code.
*
* Resetting the processing state of an application includes the following actions:
*
*
setting the application's consumer offsets for input and internal topics to zero
*
skip over all intermediate user topics (i.e., "seekToEnd" for consumers of intermediate topics)
*
deleting any topics created internally by Kafka Streams for this application
*
*
* Do only use this tool if no application instance is running.
* Otherwise, the application will get into an invalid state and crash or produce wrong results.
*
* If you run multiple application instances, running this tool once is sufficient.
* However, you need to call {@code KafkaStreams#cleanUp()} before re-starting any instance
* (to clean local state store directory).
* Otherwise, your application is in an invalid state.
*
* User output topics will not be deleted or modified by this tool.
* If downstream applications consume intermediate or output topics,
* it is the user's responsibility to adjust those applications manually if required.
*/
@InterfaceStability.Unstable
public class StreamsResetter {
private static final int EXIT_CODE_SUCCESS = 0;
private static final int EXIT_CODE_ERROR = 1;
private static final String BOOTSTRAP_SERVER_DEFAULT = "localhost:9092";
private static OptionSpec bootstrapServersOption;
private static OptionSpec bootstrapServerOption;
private static OptionSpec applicationIdOption;
private static OptionSpec inputTopicsOption;
private static OptionSpec intermediateTopicsOption;
private static OptionSpec internalTopicsOption;
private static OptionSpec toOffsetOption;
private static OptionSpec toDatetimeOption;
private static OptionSpec byDurationOption;
private static OptionSpecBuilder toEarliestOption;
private static OptionSpecBuilder toLatestOption;
private static OptionSpec fromFileOption;
private static OptionSpec shiftByOption;
private static OptionSpecBuilder dryRunOption;
private static OptionSpec helpOption;
private static OptionSpec versionOption;
private static OptionSpec commandConfigOption;
private static OptionSpecBuilder forceOption;
private final static String USAGE = "This tool helps to quickly reset an application in order to reprocess "
+ "its data from scratch.\n"
+ "* This tool resets offsets of input topics to the earliest available offset (by default), or to a specific defined position"
+ " and it skips to the end of intermediate topics (topics that are input and output topics, e.g., used by deprecated through() method).\n"
+ "* This tool deletes the internal topics that were created by Kafka Streams (topics starting with "
+ "\"-\").\n"
+ "The tool finds these internal topics automatically. If the topics flagged automatically for deletion by "
+ "the dry-run are unsuitable, you can specify a subset with the \"--internal-topics\" option.\n"
+ "* This tool will not delete output topics (if you want to delete them, you need to do it yourself "
+ "with the bin/kafka-topics.sh command).\n"
+ "* This tool will not clean up the local state on the stream application instances (the persisted "
+ "stores used to cache aggregation results).\n"
+ "You need to call KafkaStreams#cleanUp() in your application or manually delete them from the "
+ "directory specified by \"state.dir\" configuration (${java.io.tmpdir}/kafka-streams/ by default).\n"
+ "* When long session timeout has been configured, active members could take longer to get expired on the "
+ "broker thus blocking the reset job to complete. Use the \"--force\" option could remove those left-over "
+ "members immediately. Make sure to stop all stream applications when this option is specified "
+ "to avoid unexpected disruptions.\n\n"
+ "*** Important! You will get wrong output if you don't clean up the local stores after running the "
+ "reset tool!\n\n"
+ "*** Warning! This tool makes irreversible changes to your application. It is strongly recommended that "
+ "you run this once with \"--dry-run\" to preview your changes before making them.\n\n";
private OptionSet options = null;
private final List allTopics = new LinkedList<>();
public int run(final String[] args) {
return run(args, new Properties());
}
public int run(final String[] args,
final Properties config) {
int exitCode;
Admin adminClient = null;
try {
parseArguments(args);
final boolean dryRun = options.has(dryRunOption);
final String groupId = options.valueOf(applicationIdOption);
final Properties properties = new Properties();
if (options.has(commandConfigOption)) {
properties.putAll(Utils.loadProps(options.valueOf(commandConfigOption)));
}
String bootstrapServerValue = BOOTSTRAP_SERVER_DEFAULT;
if (options.has(bootstrapServerOption))
bootstrapServerValue = options.valueOf(bootstrapServerOption);
else if (options.has(bootstrapServersOption))
bootstrapServerValue = options.valueOf(bootstrapServersOption);
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServerValue);
adminClient = Admin.create(properties);
maybeDeleteActiveConsumers(groupId, adminClient);
allTopics.clear();
allTopics.addAll(adminClient.listTopics().names().get(60, TimeUnit.SECONDS));
if (dryRun) {
System.out.println("----Dry run displays the actions which will be performed when running Streams Reset Tool----");
}
final HashMap