auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
Properties getProperties() { return props; }

Target:
Without the access modifier this method will be visible inside the package but not by subclasses or classes outside the package. Is this what you want?
Source:
public synchronized void removeDomain( final String domainId ) { delegate.removeDomain( domainId ); purgeDomain( domainId ); removeDomainFromIDCache( domainId ); }

Target:
By definition, this class is session based ( SessionCachingMetadataDomainRepository ). However, this change is synchronizing on the entire MetadataRepository instance. So removing a domain for one user in their own session ends up blocking another user in another session from storing their domain or removing their model. I don't understand why this should be done at this level. If there really is synchronization required somewhere, I don't suspect it's here. What specifically happens in the multi-threaded testing without these three methods being synchronized that this solves ?
Source:
private void placeBreadcrumbFile( String folderPath, boolean success ) { String ext = ERROR; if ( success ) { ext = OK; } String breadcrumbFilename = folderPath + WI_STATUS + ext; File breadcrumb = new File( breadcrumbFilename ); try { if ( !breadcrumb.createNewFile() ) { logger.error( Messages.getInstance().getErrorString( "ActionInvokerSystemListener.ERROR_0004_COULD_NOT_WRITE_STATUS" ) ); } } catch ( IOException e ) { logger.error( Messages.getInstance().getErrorString( "ActionInvokerSystemListener.ERROR_0004_COULD_NOT_WRITE_STATUS" ) ); } }

Target:
use File object to construct the breadcrumb file: new File( folderPath, WI_STATUS + etx);
Source:
public boolean visit(MethodDeclaration node) { if (isMethod(node, "android.widget.TextView", "onDraw", "android.graphics.Canvas")) { final ASTBuilder b = this.ctx.getASTBuilder(); final Refactorings r = this.ctx.getRefactorings(); node.accept(new OnDrawTransformer(this.ctx, node)); } return VISIT_SUBTREE; }

Target:
Please remove unused `b` and `r`
Source:
public boolean visit(Assignment node) { if (ASTHelper.isSameLocalVariable(node.getLeftHandSide(), cursorSimpleName)) { return DO_NOT_VISIT_SUBTREE; } return VISIT_SUBTREE; }

Target:
Please change all uses of `ASTHelper` methods and fields to use static imports.
Source:
public void cursorError1(SQLiteDatabase db, long route_id) { Cursor cursor = db.query("TABLE_TRIPS", new String[]{ "KEY_TRIP_ID" }, "ROUTE_ID=?", new String[]{Long.toString(route_id)}, null, null, null); cursor.close(); }

Target:
Apparently they are all bad practice: * http://stackoverflow.com/questions/4144328/idiom-to-close-a-cursor * http://stackoverflow.com/questions/12950725/how-to-properly-close-a-cursor-in-android Phew! I was worried for a second. I think AutoRefactor should do the right thing here, i.e. use a `try` / `finally` block.
Source:
public String replaceToStringCallOnCurrentObjectInStringConcat() { String s1 = " " + StringSample.this + StringSample.this + this; String s2 = StringSample.this + " " + this; return s1 + s2; }

Target:
I disagree with the changes in this section. Please revert them. They highlight the fact that this code is missing one check on `extendedOperands(InfixExpression).isEmpty()`.
Source:
public Boolean doNotThrowAnyException(boolean bo) { class ClassWithBooleanField { Boolean b; } ClassWithBooleanField objWithBooleanField = new ClassWithBooleanField(); Boolean aBoolean = Boolean.TRUE; return bo ? objWithBooleanField.b : aBoolean; }

Target:
Please change the code to match how it is now replaced. I just wanted to be sure that there was no exception thrown here.
Source:
public static boolean isMethod(MethodInvocation node, String typeQualifiedName, String methodName, String... parameterTypesQualifiedNames) { if (node == null) { return false; } final IMethodBinding methodBinding = node.resolveMethodBinding(); return isMethod(methodBinding, typeQualifiedName, methodName, parameterTypesQualifiedNames); }

Target:
Please move `isMethod(MethodInvocation, ...)` above `isMethod(IMethodBinding, ...)`. This helps in reading the code top down (See Clean Code book by Uncle Bob). And will also make the diff look better.
Source:
private void updatePositionLeaderNoValidation(Metadata.LeaderAndEpoch currentLeaderAndEpoch) { if (position != null && !position.currentLeader.equals(currentLeaderAndEpoch)) { FetchPosition newPosition = new FetchPosition(position.offset, position.offsetEpoch, currentLeaderAndEpoch); transitionState(FetchStates.FETCHING, () -> { this.position = newPosition; this.nextRetryTimeMs = null; }); } }

Target:
nit: ```newPosition``` can be created lazy.
Source:
private Map<TopicPartition, Long> checkpointableOffsets() { if (state() == State.RESTORING) { return Collections.emptyMap(); } final Map<TopicPartition, Long> checkpointableOffsets = new HashMap<>(recordCollector.offsets()); for (final Map.Entry<TopicPartition, Long> entry : consumedOffsets.entrySet()) { checkpointableOffsets.putIfAbsent(entry.getKey(), entry.getValue()); } return checkpointableOffsets; }

Target:
Should we return emptyMap if we are SUSPENDED as well?
Source:
private Map<TopicPartition, Long> committedOffsetForChangelogs(final Set<TopicPartition> partitions) { final Map<TopicPartition, Long> committedOffsets; try { committedOffsets = fetchCommittedOffsets(partitions, mainConsumer); } catch (final StreamsException e) { if (e.getCause() instanceof TimeoutException) { // if it timed out we just retry next time return Collections.emptyMap(); } else { throw e; } } lastUpdateOffsetTime = time.milliseconds(); return committedOffsets; }

Target:
This seems to be a step backwards, actually. Why wrap it as a StreamsException only just to immediately unwrap it again?
Source:
public void fetchEndOffsetsShouldRethrowRuntimeExceptionAsStreamsException() { final Admin adminClient = EasyMock.createMock(AdminClient.class); EasyMock.expect(adminClient.listOffsets(EasyMock.anyObject())).andThrow(new RuntimeException()); replay(adminClient); assertThrows(StreamsException.class, () -> fetchEndOffsets(emptyList(), adminClient, 60_000L)); verify(adminClient); }

Target:
Should we pass in `MAX_VALUE` to avoid introducing test flakyness?
Source:
DeadLetterQueueReporter(KafkaProducer<byte[], byte[]> kafkaProducer, SinkConnectorConfig connConfig, ConnectorTaskId id, ErrorHandlingMetrics errorHandlingMetrics) { Objects.requireNonNull(kafkaProducer); Objects.requireNonNull(connConfig); Objects.requireNonNull(id); Objects.requireNonNull(errorHandlingMetrics); this.kafkaProducer = kafkaProducer; this.connConfig = connConfig; this.connectorTaskId = id; this.errorHandlingMetrics = errorHandlingMetrics; this.dlqTopicName = connConfig.dlqTopicName(); }

Target:
Should we trim this? ```suggestion this.dlqTopicName = connConfig.dlqTopicName().trim(); ```
Source:
public InternalSinkRecord(ConsumerRecord<byte[], byte[]> originalRecord, String topic, int partition, Schema keySchema, Object key, Schema valueSchema, Object value, long kafkaOffset, Long timestamp, TimestampType timestampType, Iterable<Header> headers) { super(topic, partition, keySchema, key, valueSchema, value, kafkaOffset, timestamp, timestampType, headers); this.originalRecord = originalRecord; }

Target:
Let's make this protected. ```suggestion protected InternalSinkRecord(ConsumerRecord<byte[], byte[]> originalRecord, String topic, ```
Source:
public void awaitAllFutures() { Future<?> future = null; while ((future = futures.poll()) != null) { try { future.get(); } catch (InterruptedException | ExecutionException e) { log.error("Encountered an error while awaiting an errant record future's completion."); throw new ConnectException(e); } } }

Target:
nit: initialization is not required
Source:
public boolean test(R record) { return record.key() == null; }

Target:
I think we want to check the value instead of the key here? ```suggestion return record.value() == null; ```
Source:
public JmxReporter(String prefix) { Objects.requireNonNull(prefix); this.prefix = prefix; }

Target:
Should we throw an exception here, or just replace the null value with an empty string?
Source:
public boolean equals(Object other) { if (this == other) { return true; } if (other == null || !(other instanceof Features)) { return false; } final Features that = (Features) other; return Objects.equals(this.features, that.features); }

Target:
we don't need to check `other == null` here, the next condition check covers it.
Source:
public NewTopicBuilder partitions(int numPartitions) { if (numPartitions == NO_PARTITIONS) { return defaultPartitions(); } this.numPartitions = numPartitions; return this; }

Target:
this check seems to replicate a bit what's happening in `NewTopic`. Should we just pass the value, given that we have validations too already?
Source:
public NewTopicBuilder replicationFactor(short replicationFactor) { if (replicationFactor == NO_REPLICATION_FACTOR) { return defaultReplicationFactor(); } this.replicationFactor = replicationFactor; return this; }

Target:
same question as above
Source:
private void waitForRunning(final List<KeyValue<KafkaStreams.State, KafkaStreams.State>> observed) throws Exception { waitForCondition( () -> observed.get(observed.size() - 1).equals(new KeyValue<>(State.REBALANCING, State.RUNNING)), MAX_WAIT_TIME_MS, () -> "Client did not startup on time. Observers transitions: " + observed ); }

Target:
Seems this could fail if `observed` is empty? Also, could we simplify this? We are only interested in `RUNNING ` state, right? So no need to check the previous state?
Source:
protected void configureHttpResponsHeaderFilter(ServletContextHandler context) { String headerConfig = config.getString(WorkerConfig.RESPONSE_HTTP_HEADERS_CONFIG); log.debug("headerConfig : " + headerConfig); String[] configs = StringUtil.csvSplit(headerConfig); Arrays.stream(configs) .forEach(WorkerConfig::validateHttpResponseHeaderConfig); FilterHolder headerFilterHolder = new FilterHolder(HeaderFilter.class); headerFilterHolder.setInitParameter("headerConfig", headerConfig); context.addFilter(headerFilterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); }

Target:
Is this line really necessary? Isn't the `response.http.headers.config` property already logged at INFO level when the worker starts up, via the WorkerConfig (or rather DistributedConfig or StandaloneConfig) constructor?
Source:
public RemoveMembersFromConsumerGroupOptions() { this.members = Collections.emptySet();; }

Target:
nit: extra semi-colon
Source:
public Map<String, String> connectorConfig(String connName) { FutureCallback<Map<String, String>> connectorConfigCallback = new FutureCallback<>(); herder.connectorConfig(connName, connectorConfigCallback); try { Map<String, String> result = connectorConfigCallback.get(herderRequestTimeoutMs, TimeUnit.MILLISECONDS); return new HashMap<>(result); } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new ConnectException( String.format("Failed to retrieve configuration for connector '%s'", connName), e ); } }

Target:
```suggestion return new HashMap<>(connectorConfigCallback.get(herderRequestTimeoutMs, TimeUnit.MILLISECONDS);); ```
Source:
public Map<String, String> connectorConfig(String connName) { FutureCallback<Map<String, String>> connectorConfigCallback = new FutureCallback<>(); herder.connectorConfig(connName, connectorConfigCallback); try { return new HashMap<>(connectorConfigCallback.get(herderRequestTimeoutMs, TimeUnit.MILLISECONDS);); } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new ConnectException( String.format("Failed to retrieve configuration for connector '%s'", connName), e ); } }

Target:
My bad. My suggestion inserted a typo. At least I saw it before I start the build. ```suggestion return new HashMap<>(connectorConfigCallback.get(herderRequestTimeoutMs, TimeUnit.MILLISECONDS)); ```
Source:
public void shouldWarnAboutRocksDBConfigSetterIsNotGuaranteedToBeBackwardsCompatible() { props.setProperty(StreamsConfig.ROCKSDB_CONFIG_SETTER_CLASS_CONFIG, TestRocksDbConfigSetter.class.getName()); final LogCaptureAppender appender = LogCaptureAppender.createAndRegister(); try { new KafkaStreams(new StreamsBuilder().build(), props, supplier, time); } finally { LogCaptureAppender.unregister(appender); } assertThat(appender.getMessages(), hasItem("stream-client [" + CLIENT_ID + "] " + "RocksDB's version will be bumped to version 6+ via KAFKA-8897 in a future release. " + "If you use `org.rocksdb.CompactionOptionsFIFO#setTtl(long)` or `#ttl()` you will need to rewrite " + "your code after KAFKA-8897 is resolved and set TTL via `org.rocksdb.Options` " + "(or `org.rocksdb.ColumnFamilyOptions`).")); }

Target:
Seems like this appender could also be scoped to the class that logs the message we want to capture, right?
Source:
void offer(final UUID client) { if (uniqueClients.contains(client)) { clientsByTaskLoad.remove(client); } clientsByTaskLoad.offer(client); uniqueClients.add(client); }

Target:
prop: I would not add the client if it is already contained in the set.
Source:
public void close() { ConnectException e = new ConnectException("Failed to close all reporters"); for (ErrorReporter reporter : reporters) { try { reporter.close(); } catch (Throwable t) { e.addSuppressed(t); } } if (e.getSuppressed().length > 0) { throw e; } }

Target:
Here's a nice blog re: exception costs and when they occur: https://shipilev.net/blog/2014/exceptional-performance
Source:
static void throwSuppressed(final String message, final LinkedList<RuntimeException> suppressed) { if (suppressed != null && !suppressed.isEmpty()) { final RuntimeException firstCause = suppressed.pollFirst(); final RuntimeException toThrow = new RuntimeException(message, firstCause); for (final RuntimeException e : suppressed) { toThrow.addSuppressed(e); } throw toThrow; } }

Target:
The first exception will be the "cause"
Source:
static LinkedList<RuntimeException> executeAll(final Runnable... actions) { LinkedList<RuntimeException> suppressed = null; for (final Runnable action : actions) { try { action.run(); } catch (final RuntimeException exception) { if (suppressed == null) { suppressed = new LinkedList<>(); } suppressed.add(exception); } } return suppressed; }

Target:
Why not make it `final` and return an empty list? It seems better to avoid using `null` if possible
Source:
public void close() { lock.writeLock().lock(); try { final LinkedList<RuntimeException> suppressed = executeAll( () -> cache.flush(cacheName), () -> cache.close(cacheName), wrapped()::close ); if (suppressed != null && !suppressed.isEmpty()) { throwSuppressed("Caught an exception while closing caching key value store for store " + name(), suppressed); } } finally { lock.writeLock().unlock(); } }

Target:
The check itself (ie, for `isEmpty()` makes sense) but I would still try to avoid the `null`
Source:
private boolean shouldUsePreviousAssignment() { if (previousAssignmentIsValid()) { final int previousAssignmentBalanceFactor = computeBalanceFactor(clientStates.values(), statefulTasks, ClientState::prevActiveTasks); return previousAssignmentBalanceFactor <= configs.balanceFactor; } else { return false; } }

Target:
Q: Why do we only consider stateful tasks in `computeBalanceFactor()`?
Source:
public StreamsProducer(final Producer<byte[], byte[]> producer, final StreamThread.ProcessingMode processingMode, final LogContext logContext) { this.producer = Objects.requireNonNull(producer, "producer cannot be null"); this.processingMode = Objects.requireNonNull(processingMode, "processingMode cannot be null"); this.eosEnabled = processingMode == EXACTLY_ONCE_ALPHA || processingMode == EXACTLY_ONCE_BETA; log = Objects.requireNonNull(logContext, "logContext cannot be null").logger(getClass()); logPrefix = logContext.logPrefix().trim(); }

Target:
nit: now that we are passing in the processingMode, we could potentially use this check as a helper function as: ``` private boolean0 eosEnabled() { processingMode == EXACTLY_ONCE_ALPHA || processingMode == EXACTLY_ONCE_BETA; } ```
Source:
public void enforceRestoreActive() { if (state != ChangelogReaderState.ACTIVE_RESTORING) { log.debug("Transiting to restore active tasks: {}", changelogs); // pause all partitions that are for standby tasks from the restore consumer pauseChangelogsFromRestoreConsumer(standbyRestoringChangelogs()); } state = ChangelogReaderState.ACTIVE_RESTORING; }

Target:
We can move the line 290 inside as well?
Source:
ClientMetadata(final String endPoint) { // get the host info if possible hostInfo = endPoint != null ? HostInfo.buildFromEndpoint(endPoint) : null; // initialize the consumer memberIds consumers = new HashSet<>(); // initialize the client state state = new ClientState(); }

Target:
nit: I feel we could internalize the hostInfo creation, including the null check: `hostInfo = HostInfo.buildFromEndpoint(endPoint);`
Source:
private Map<String, List<TaskId>> expectedAssignmentForTwoClients(final List<TaskId> assignedTasksForClient1, final List<TaskId> assignedTasksForClient2) { return mkMap( mkEntry(CLIENT_1, assignedTasksForClient1), mkEntry(CLIENT_2, assignedTasksForClient2) ); }

Target:
These utility methods can be static.
Source:
public RawAndDeserializedValue<V> getWithBinary(final K key) { try { final byte[] serializedValue = wrapped().get(keyBytes(key)); return new RawAndDeserializedValue<V>(serializedValue, maybeMeasureLatency(() -> outerValue(serializedValue), time, getSensor)); } catch (final ProcessorStateException e) { final String message = String.format(e.getMessage(), key); throw new ProcessorStateException(message, e); } }

Target:
Ah, missed this the last time though. We should also perform the `wrapped().get` inside the lambda, so that the time to perform the get is included in the latency.
Source:
public void shouldGetWithBinary() { expect(inner.get(keyBytes)).andReturn(valueAndTimestampBytes); inner.put(eq(keyBytes), aryEq(valueAndTimestampBytes)); expectLastCall(); init(); metered.put(key, valueAndTimestamp); final RawAndDeserializedValue<String> valueWithBinary = metered.getWithBinary(key); assertEquals(valueWithBinary.value, valueAndTimestamp); assertEquals(valueWithBinary.serializedValue, valueAndTimestampBytes); }

Target:
Since you mocked the inner store `get`, you shouldn't need to actually do a `put`, right?
Source:
public void shouldNotPutIfSameValuesAndGreaterTimestamp() { expectLastCall(); init(); metered.put(key, valueAndTimestamp); final ValueAndTimestampSerde<String> stringSerde = new ValueAndTimestampSerde<>(Serdes.String()); final byte[] encodedOldValue = stringSerde.serializer().serialize("TOPIC", valueAndTimestamp); final ValueAndTimestamp<String> newValueAndTimestamp = ValueAndTimestamp.make("value", 98L); assertFalse(metered.putIfDifferentValues(key, newValueAndTimestamp, encodedOldValue)); verify(inner); }

Target:
```suggestion ```
Source:
public void shouldTryToLockValidTaskDirsAtRebalanceStart() throws IOException { expectLockObtainedFor(taskId01); expectLockFailedFor(taskId10); makeTaskFolders( "0_1", "1_0", "dummy" ); replay(stateDirectory); taskManager.handleRebalanceStart(singleton("topic")); verify(stateDirectory); assertThat(taskManager.lockedTaskDirectories(), is(singleton(taskId01))); }

Target:
prop: I would use `taskId01.toString()` here, since you are not testing the `taskId01.toString()` method. Our assumption is that the folder has a name that is equal to the result of `taskId01.toString()` and not `0_1`.
Source:
private static void expectConsumerAssignmentPaused(final Consumer<byte[], byte[]> consumer) { final Set<TopicPartition> assignment = singleton(new TopicPartition("assignment", 0)); expect(consumer.assignment()).andReturn(assignment); consumer.pause(assignment); expectLastCall(); }

Target:
prop: You can remove this line. You only need `expectLastCall()` if you need to do some further expectation settings on a call that returns `void`, e.g., `expectLastCall().times(3)`
Source:
public ConsumerGroupListing(String groupId, boolean isSimpleConsumerGroup, Optional<ConsumerGroupState> state) { Objects.requireNonNull(state); this.groupId = groupId; this.isSimpleConsumerGroup = isSimpleConsumerGroup; this.state = state; }

Target:
nit: usually we would write this is `this.state = requireNonNull(state);`
Source:
public void postCommit() { switch (state()) { case RUNNING: case RESTORING: commitNeeded = false; commitRequested = false; if (eosDisabled) { stateMgr.checkpoint(checkpointableOffsets()); } log.info("Committed"); break; default: throw new IllegalStateException("Illegal state " + state() + " while post committing active task " + id); } }

Target:
This is not introduced in this PR, but: while thinking about it, I realized for RESTORING state we do not need to rely on eosDisabled to checkpoint, in fact we can always checkpoint during RESTORING here.
Source:
private static String getMetricsVersion(final Properties config) { if (config.containsKey(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG)) { return config.getProperty(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG); } return DEFAULT_METRICS_VERSION; }

Target:
req: Please use the following if you still need this method. ```suggestion return config.getProperty(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG, DEFAULT_METRICS_VERSION); ```
Source:
public void onPartitionsLost(final Collection<TopicPartition> partitions) { log.info("at state {}: partitions lost due to missed rebalance.\n" + "\tlost active tasks: {}\n" + "\tlost assigned standby tasks: {}\n", streamThread.state(), partitions, taskManager.activeTaskIds(), taskManager.standbyTaskIds()); final long start = time.milliseconds(); try { // close all active tasks as lost but don't try to commit offsets as we no longer own them taskManager.handleLostAll(); } catch (final Exception e) { throw new TaskMigratedException("Error caught during partitions lost", e); } finally { log.info("partitions lost took ms.", time.milliseconds() - start); } }

Target:
Hmm.. do we want to capture all Exceptions as TaskMigratedException? I actually think it's okay to not capture anything here since only StreamsException or TaskMigratedException would be thrown here.
Source:
public void testIsHeartbeatTopic() throws InterruptedException, TimeoutException { MirrorClient client = new FakeMirrorClient(); assertTrue(client.isHeartbeatTopic("heartbeats")); assertTrue(client.isHeartbeatTopic("source1.heartbeats")); assertTrue(client.isHeartbeatTopic("source2.source1.heartbeats")); assertFalse(client.isHeartbeatTopic("heartbeats!")); assertFalse(client.isHeartbeatTopic("!heartbeats")); assertFalse(client.isHeartbeatTopic("source1heartbeats")); assertFalse(client.isHeartbeatTopic("source1-heartbeats")); client.close(); }

Target:
We are calling `close` even though it doesn't do anything just to avoid warnings? Where are the warnings coming from?
Source:
public void shutdown() { synchronized (this) { log.info("Scheduled shutdown for {}", this); stopping = true; notify(); } }

Target:
if you endup locking the whole method, there's no reason to use a block, you can use `synchronized` on the method. Applies here and any similar method.
Source:
private void addChangelogsToRestoreConsumer(final Set<TopicPartition> partitions) { final Set<TopicPartition> assignment = new HashSet<>(restoreConsumer.assignment()); // the current assignment should not contain any of the new partitions if (assignment.removeAll(partitions)) { throw new IllegalStateException("The current assignment " + restoreConsumer.assignment() + " " + "already contains some of the new partitions " + partitions); } assignment.addAll(partitions); restoreConsumer.assign(assignment); log.debug("Added partitions to the restore consumer, current assignment is {}", partitions, assignment); }

Target:
Does the consumer not log this already within `assign(...)` ?
Source:
private void waitUntilMirrorMakerIsRunning(EmbeddedConnectCluster connectCluster, Set<String> connNames) throws InterruptedException { for (String connector : connNames) { TestUtils.waitForCondition(() -> areConnectorAndTasksRunning(connectCluster, connector, MIN_TASKS), "Timed out trying to verify connector " + connector + " was up!" ); } }

Target:
nit: unneeded newline
Source:
void addMetrics(Collection<KafkaMetric> metrics) { checkLocked(); if (!beanMetrics.isEmpty()) { unregister(); } for (KafkaMetric metric : metrics) { beanMetrics.put(metric.metricName().name(), metric); } register(); }

Target:
Are we assuming that `metrics` is non-empty?
Source:
public CreateTopicsResult(Map<String, KafkaFuture<TopicMetadataAndConfig>> futures) { this.futures = futures; }

Target:
What about making it protected instead and mock a sub-class? `CreateTopicsResults` is part of the public API and we might not want to make them public (would this be a public API change?)
Source:
public void materialize() { this.queryableName = storeName; if (tableProcessorNode != null) { tableProcessorNode.setStoreNeeded(true); } }

Target:
I don't think we need all this, if we delay adding the store to `writeToTopology()`
Source:
private boolean isProtocolNameInconsistent(String protocolName) { return protocolName != null && !protocolName.equals(generation().protocol); }

Target:
Should we change `Generation.protocol` to `protocolName` as well?
Source:
private Deque<NetworkClient.InFlightRequest> requestQueue(String node) { Deque<NetworkClient.InFlightRequest> reqs = requests.get(node); if (reqs == null || reqs.isEmpty()) { throw new IllegalStateException("There are no in-flight requests for node " + node); } return reqs; }

Target:
Let's remove the brace changes please.
Source:
public void shouldThrowOnSendOffsetsToTransactionIfTransactionsNotInitialized() { buildMockProducer(true); String groupId = null; producer.sendOffsetsToTransaction(null, groupId); }

Target:
To ensure that we are really testing the state machine as expected, we should provide a valid groupId. Similarly below.
Source:
private boolean isFatalException(Errors error) { return error == Errors.TRANSACTIONAL_ID_AUTHORIZATION_FAILED || error == Errors.INVALID_PRODUCER_EPOCH || error == Errors.UNSUPPORTED_FOR_MESSAGE_FORMAT || error == Errors.FENCED_INSTANCE_ID; }

Target:
Wait, how did we end up back here? I thought we agreed this should not be fatal for the producer? I think it should have a separate branch above, similar to the handling of `GROUP_AUTHORIZATION_FAILED`.
Source:
public boolean hasNext() { if (this.peek != null) { return true; } if (!this.iterator.hasNext()) { this.iterator = this.iterable.iterator(); } return this.iterator.hasNext(); }

Target:
Why change the logic since the answer is always `true`?
Source:
public boolean hasNext() { if (this.hasPeek) { return true; } if (!this.iterator.hasNext()) { this.iterator = this.iterable.iterator(); } return true; }

Target:
Should simply return true here, right? As written now it is adjusting internal state, and it is best to isolate state manipulation to as few places as possible.
Source:
public T next() { final T next = this.nextValue; this.nextValue = advance(); return next; }

Target:
no need to use `this.` outside the constructor. Here and below
Source:
synchronized Set<String> metadataTopics() { Set<String> topics = new HashSet<>(groupSubscription); topics.addAll(subscription); return topics; }

Target:
It might borderline overkill, but I'm considering if we could avoid the copy here with logic like the following: ``` if (!groupSubscription.isEmpty()) return groupSubscription; else return subscription; ``` Basically relying on the the group subscription being a superset of the local subscription when it is defined.
Source:
public Iterator<Header> iterator() { return (headers == null) ? Collections.emptyIterator() : headers.iterator(); }

Target:
nit: parens unnecessary
Source:
public ConsumerGroupMetadata groupMetadata() { return new ConsumerGroupMetadata(groupMetadata); }

Target:
Why we want to return a clone of the groupMetadata? If we can declare all the fields as final, and whenever we update we always create a new ConsumerGroupMetadata object, then I think we can safely return the cached object directly.
Source:
public void testThreadNameWithoutNumberDemon() { Thread daemonThread = ThreadUtils.createThreadFactory(THREAD_NAME, true).newThread(EMPTY_RUNNABLE); try { Assert.assertEquals(daemonThread.getName(), THREAD_NAME); Assert.assertTrue(daemonThread.isDaemon()); } finally { try { daemonThread.join(); } catch (InterruptedException e) { // can be ignored e.printStackTrace(); } } }

Target:
Maybe we don't need to print the stack trace if we can ignore it
Source:
private ConsumerCoordinator buildCoordinator(final GroupRebalanceConfig rebalanceConfig, final Metrics metrics, final List<ConsumerPartitionAssignor> assignors, final boolean autoCommitEnabled) { return new ConsumerCoordinator( rebalanceConfig, new LogContext(), consumerClient, assignors, metadata, subscriptions, metrics, "consumer" + groupId, time, autoCommitEnabled, autoCommitIntervalMs, false, null); }

Target:
Why do we pass `false` here?
Source:
public void clearChildrenProducerBatch() { childrenProducerBatch.clear(); }

Target:
I'm wondering if we should take care of concurrent modification for this list. We should, maybe?
Source:
public void awaitFlushCompletion() throws InterruptedException { try { for (ProducerBatch batch : this.incomplete.copyAll()) { batch.produceFuture.await(); waitForPossibleSplittedBatches(batch); batch.clearChildrenProducerBatch(); } } finally { this.flushesInProgress.decrementAndGet(); } }

Target:
It seems like it might be cleaner if we move all the waitForPossibleSplittedBatches logic into a `ProducerBatch.await` method that performs the await on its produceFuture as well as on the child batches.
Source:
private void addChildrenProducerBatch(final ProducerBatch batch) { if (childrenProducerBatch == null) { childrenProducerBatch = new ArrayList<>(); } childrenProducerBatch.add(batch); }

Target:
This should also be synchronized
Source:
public void shouldNotAllowInvalidTopicNames() { final String invalidName = "not:valid"; final TopologyException e = assertThrows(TopologyException.class, () -> Materialized.as(invalidName)); assert e.getMessage().equals("Invalid topology: Name \"" + invalidName + "\" is illegal, it contains a character other than " + "ASCII alphanumerics, '.', '_' and '-'"); }

Target:
We should not use Java `assert` statement but proper unit testing asserts, ie, `assertThat(e.getMessage(), equalTo("..."));`
Source:
public void close() { log.debug("Closing record collector"); maybeAbortTxn(); if (eosEnabled) { try { producer.close(); } catch (final KafkaException e) { throw new StreamsException("Caught a recoverable exception while closing", e); } producer = null; } checkForException(); }

Target:
req: This is unnecessary
Source:
public KTable<Windowed<K>, V> aggregate(final Initializer<V> initializer, final Merger<? super K, V> sessionMerger) { return aggregate(initializer, sessionMerger, Materialized.with(keySerde, null)); }

Target:
the `keySerde` is set to `null` in the constructor, hence, I am wondering if we should pass in `null`, here, too? (similar question for `TimeWindowedCogroupedKStreamImpl`)
Source:
public ConfigEntry(String name, String value, ConfigSource source, boolean isSensitive, boolean isReadOnly, List<ConfigSynonym> synonyms) { Objects.requireNonNull(name, "name should not be null"); this.name = name; this.value = value; this.source = source; this.isSensitive = isSensitive; this.isReadOnly = isReadOnly; this.synonyms = synonyms; }

Target:
This is a public API, so let's not change it here. If you need a `ConfigEntry` with synonyms for the test, create a static method in `ConfigTest` that will make it. That will keep the change confined to the test code.
Source:
public <W extends Window> TimeWindowedCogroupedKStream<K, VOut> windowedBy(final Windows<W> windows) { Objects.requireNonNull(windows, "windows can't be null"); return new TimeWindowedCogroupedKStreamImpl<>(windows, builder, sourceNodes, name, keySerde, null, aggregateBuilder, streamsGraphNode, groupPatterns); }

Target:
nit: move `windows` to next line
Source:
public void registerClientInformation(final ClientInformation clientInformation) { if (this.clientInformation != null) { sensors.connectionsByClient.decrement(this.clientInformation); } this.clientInformation = clientInformation; sensors.connectionsByClient.increment(clientInformation); }

Target:
If this already exists, we should check to see if it's equal to the new information we wanted to register. If it is, we can skip the decrement metric + increment metric dance.
Source:
private void flushAndCheckpointState() { try { stateMgr.flush(); } catch (final ProcessorStateException e) { if (e.getCause() instanceof ProducerFencedException) { log.warn("Caught a retriable Kafka exception while flushing state. Initiating a rebalance to attempt recovery.", e); throw new TaskMigratedException(this, e); } } stateMgr.checkpoint(Collections.emptyMap()); }

Target:
The same argument as for `GlobalStateStore` should apply here -- StandbyTask don't write but only read, hence, no `ProducerFencedException` could ever happen here.
Source:
boolean hasRestoringTasks() { if (restoring.isEmpty()) { restoredPartitions.clear(); restoringByPartition.clear(); } return !restoring.isEmpty(); }

Target:
It seems a bit awkward to have a `hasXXX` call have side effects. Although I do understand the motivation here, I guess this is something to revisit during a subsequent refactoring/follow-on PR.
Source:
public void testUpdateMetadataAllowedImmediatelyAfterBootstrap() { MockTime time = new MockTime(); Metadata metadata = new Metadata(refreshBackoffMs, metadataExpireMs, new LogContext(), new ClusterResourceListeners()); metadata.bootstrap(Collections.singletonList(new InetSocketAddress("localhost", 9002))); metadata.requestUpdate(); assertEquals(0, metadata.timeToAllowUpdate(time.milliseconds())); assertEquals(0, metadata.timeToNextUpdate(time.milliseconds())); }

Target:
`bootstrap()` itself should mandate an update, right? Do we need to call `requestUpdate`?
Source:
public void reconfigure(Map<String, ?> configs) { synchronized (LOCK) { this.mbeanPredicate = JmxReporter.compilePredicate(configPrefix, configs); mbeans.forEach((name, mbean) -> { if (mbeanPredicate.test(name)) { reregister(mbean); } else { unregister(mbean); } }); } }

Target:
I might be missing something, but in both cases, you just want to use regular expressions, right? There is no need to mess around with predicate functions.
Source:
public void shouldGetLateRecordDropSensor() { final String metricNamePrefix = "late-record-drop"; final String descriptionOfCount = "The total number of dropped late records"; final String descriptionOfRate = "The average number of dropped late records per second"; if (builtInMetricsVersion == Version.FROM_0100_TO_24) { setUpThroughputSensor( metricNamePrefix, descriptionOfRate, descriptionOfCount, RecordingLevel.INFO ); } else { expect(streamsMetrics.nodeLevelSensor( THREAD_ID, TASK_ID, PROCESSOR_NODE_ID, metricNamePrefix, RecordingLevel.INFO )).andReturn(expectedSensor); } verifySensor(() -> ProcessorNodeMetrics.lateRecordDropSensor(THREAD_ID, TASK_ID, PROCESSOR_NODE_ID, streamsMetrics)); }

Target:
What is `expectedSensor` used here since it is just a mock? I feel we can just state that we expect it to be called but not specify returning anything?
Source:
public NewPartitionReassignment(List<Integer> targetReplicas) { this.targetReplicas = Collections.unmodifiableList(new ArrayList<>(targetReplicas)); }

Target:
Does this need to be public? Making it private forces use of `of` method, which I think is good.
Source:
public static synchronized void unregisterAppInfo(String prefix, String id, Metrics metrics) { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); try { id = id == null ? "" : id; ObjectName name = new ObjectName(prefix + ":type=app-info,id=" + Sanitizer.jmxSanitize(id)); if (server.isRegistered(name)) server.unregisterMBean(name); unregisterMetrics(metrics); } catch (JMException e) { log.warn("Error unregistering AppInfo mbean", e); } }

Target:
Hmm, maybe we can move this check into the KafkaConsumer's catch block before `close` also.
Source:
public void add(TopicPartition topicPartition, PartitionData data) { if (sessionPartitions.containsKey(topicPartition)) { altered.add(topicPartition); } else { added.add(topicPartition); } next.put(topicPartition, data); sessionPartitions.put(topicPartition, data); }

Target:
I think you may be able to `.put` the added PartitionData into sessionPartitions up front, and capture the value returned by the `.put`, as it will return the previous value. If the previous value was null, then we add to added, if it was not null we add it to altered. This will save us the additional containsKey check.
Source:
public void remove(TopicPartition topicPartition) { removed.add(topicPartition); next.remove(topicPartition); sessionPartitions.remove(topicPartition); }

Target:
Will any of these partitions be in next? My assumption is that they would not be in partitionStates and would not be in next if they are being removed (especially if we get the locking right). Of course, they would be in sessionPartitions.
Source:
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable throwable) { ApiError error = ApiError.fromThrowable(throwable); DescribeAclsResponseData response = new DescribeAclsResponseData() .setThrottleTimeMs(throttleTimeMs) .setErrorCode(error.error().code()) .setErrorMessage(error.message()) .setResources(Collections.emptyList()); return new DescribeAclsResponse(response); }

Target:
This is not necessary, right?
Source:
public int bytesRead() { if (buffer == null) return size.position(); return payload().position() + size.position(); }

Target:
Looks odd to use `buffer` a couple of lines above and `payload()` here since both refer to the same thing.
Source:
@Override public Void deserialize(String topic, byte[] data) { if (data != null) throw new IllegalArgumentException("Data should be null for a VoidDeserializer."); return null; }

Target:
nit: put `@Override` on its own line
Source:
public void close() { this.adminClient.close(Duration.ZERO); }

Target:
The default implementation of `close()` is to wait forever. What's the reasoning for using `Duration.ZERO` here?
Source:
private ConsumerRebalanceListener getConsumerRebalanceListener(final KafkaConsumer<String, String> consumer) { return new ConsumerRebalanceListener() { @Override public void onPartitionsRevoked(Collection<TopicPartition> partitions) { } @Override public void onPartitionsAssigned(Collection<TopicPartition> partitions) { // set initial position so we don't need a lookup for (TopicPartition partition : partitions) consumer.seek(partition, 0); } @Override public void onPartitionsLost(Collection<TopicPartition> partitions) { System.out.println("hit partitions lost " + partitions); } }; }

Target:
Is this an outdated debug statement? If not, maybe add a similar line to `onPartitionsRevoked()` ?
Source:
public ConnectionMetadata get(String connectionId) { return connections.get(connectionId); }

Target:
This is wrong. Unsynchronized access to a map could cause more than just "stale or inconsistent" data. It could cause null pointer exceptions or other issues. We cannot access this without synchronization.
Source:
OptionalLong lastAckedOffset(TopicPartition topicPartition) { TopicPartitionEntry entry = topicPartitions.get(topicPartition); if (entry != null && entry.lastAckedOffset != ProduceResponse.INVALID_OFFSET) return OptionalLong.of(entry.lastAckedOffset); else return OptionalLong.empty(); }

Target:
nit: better to make functions private if we do not have intention to have them accessed by other classes
Source:
private boolean isNextSequenceForUnresolvedPartition(TopicPartition topicPartition, int sequence) { if (this.hasUnresolvedSequence(topicPartition)) { return sequence == this.partitionsWithUnresolvedSequences.get(topicPartition); } return false; }

Target:
Could be simplified as: `hasUnresolvedSequence(topicPartition) && sequence == this.partitionsWithUnresolvedSequences.get(topicPartition)`
Source:
public void testInitProducerIdRequest() throws Exception { final long producerId = 343434L; TransactionManager transactionManager = createTransactionManager(); setupWithTransactionState(transactionManager); prepareAndReceiveInitProducerId(producerId, Errors.NONE); assertTrue(transactionManager.hasProducerId()); assertEquals(producerId, transactionManager.producerIdAndEpoch().producerId); assertEquals((short) 0, transactionManager.producerIdAndEpoch().epoch); }

Target:
nit: the exception is no longer thrown.
Source:
private int maybeUpdateLastAckedSequence(TopicPartition topicPartition, int sequence) { if (sequence > lastAckedSequence(topicPartition).orElse(NO_LAST_ACKED_SEQUENCE_NUMBER)) { topicPartitionBookkeeper.getPartition(topicPartition).lastAckedSequence = sequence; return sequence; } return NO_LAST_ACKED_SEQUENCE_NUMBER; }

Target:
This doesn't seem right. `lastAckedSequence(topicPartition)` may exist.
Source:
private int maybeUpdateLastAckedSequence(TopicPartition topicPartition, int sequence) { if (sequence > lastAckedSequence(topicPartition).orElse(NO_LAST_ACKED_SEQUENCE_NUMBER)) { topicPartitionBookkeeper.getPartition(topicPartition).lastAckedSequence = sequence; return sequence; } return lastAckedSequence(topicPartition).orElse(NO_LAST_ACKED_SEQUENCE_NUMBER); }

Target:
nit: can we just put this in a variable?
Source:
int maybeCommitActiveTasksPerUserRequested() { if (!rebalanceInProgress) { return active.maybeCommitPerUserRequested(); } return -1; }

Target:
nit: Ternary operator seems easier to use.
Source:
int commitAll() { if (rebalanceInProgress) { return -1; } final int committed = active.commit(); return committed + standby.commit(); }

Target:
As long as we have separate tags for standby and primary tasks commit, I don't think the metrics will be messed up.
Source:
public void shouldAllowCleanupBeforeStartAndAfterClose() { final KafkaStreams streams = new KafkaStreams(new StreamsBuilder().build(), props, supplier, time); try { streams.cleanUp(); streams.start(); } finally { streams.close(); } streams.cleanUp(); }

Target:
What's up with this last one? Should it be in the finally block?
Source:
public PluginClassLoader pluginClassLoader(String name) { if (!PluginUtils.shouldLoadInIsolation(name)) { return null; } SortedMap<PluginDesc<?>, ClassLoader> inner = pluginLoaders.get(name); if (inner == null) { return null; } ClassLoader pluginLoader = inner.get(inner.lastKey()); if (!(pluginLoader instanceof PluginClassLoader)) { return null; } return (PluginClassLoader) pluginLoader; }

Target:
`return pluginLoader instanceof PluginClassLoader ? (PluginClassLoader) pluginLoader : null;` wdyt? keeps the instanceof closer to the cast and it's concise.
Source:
public RebalanceInProgressException() { super("Request cannot be completed since the consumer group is rebalancing. This can happen when a group has started, " + "but not finished rebalancing before the request is sent. You can try completing the rebalance " + "by calling poll() and then retry the operation"); }

Target:
Why not have this extend `RetriableCommitFailedException`?
Source:
public AlterConsumerGroupOffsetsResult(KafkaFuture<Map<TopicPartition, Errors>> future) { this.future = future; }

Target:
Usually we use default access constructors for these Result types. Similarly for `ListOffsetsResult`.
Source:
public boolean shouldRefreshCoordinator(Errors error) { return error == Errors.COORDINATOR_LOAD_IN_PROGRESS || error == Errors.COORDINATOR_NOT_AVAILABLE; }

Target:
This method could be static since it does not depend on any local state. Same for `hasCoordinatorMoved`.
Source:
public ListOffsetsResultInfo(long offset, long timestamp, Optional<Integer> leaderEpoch) { this.offset = offset; this.timestamp = timestamp; this.leaderEpoch = leaderEpoch; }

Target:
Default access here too?
Source:
public <VO, VR> KStream<K, VR> join(final KStream<K, VO> otherStream, final ValueJoiner<? super V, ? super VO, ? extends VR> joiner, final JoinWindows windows, final StreamJoined<K, V, VO> streamJoined) { return doJoin( otherStream, joiner, windows, streamJoined, new KStreamImplJoin(this, false, false) ); }

Target:
nit: `doJoin(otherStream,` (no new line)
Source:
public void forEachAssignedPartition(Consumer<TopicPartition> consumer) { this.assignment.partitionSet().forEach(consumer); }

Target:
Should this be synchronized?
Source:
private void resetOffsetIfNeeded(TopicPartition partition, OffsetResetStrategy requestedResetStrategy, ListOffsetData offsetData) { SubscriptionState.FetchPosition position = new SubscriptionState.FetchPosition( offsetData.offset, offsetData.leaderEpoch, metadata.leaderAndEpoch(partition)); offsetData.leaderEpoch.ifPresent(epoch -> metadata.updateLastSeenEpochIfNewer(partition, epoch)); subscriptions.maybeSeekUnvalidated(partition, position, requestedResetStrategy); }

Target:
Mentioned offline, but there is actually a behavioral change here. Previously we didn't validate after an offset reset in any case, but now I think we might if the offset returned from the ListOffset request has an epoch. I think the simple thing to do is to use `Optional.empty()` above when defining `position`.
Source:
private boolean isMuted(TopicPartition tp, long now) { Long throttleUntilTime = muted.get(tp); boolean result = throttleUntilTime != null && throttleUntilTime > now; if (!result) muted.remove(tp); return result; }

Target:
This should probably be `throttleUntilTime != null && throttleUntilTime <= now` or something?
Source:
private boolean isMuted(TopicPartition tp, long now) { Long throttleUntilTime = muted.get(tp); boolean unmute = throttleUntilTime != null && now >= throttleUntilTime; if (unmute) muted.remove(tp); return unmute; }

Target:
I think this got inverted, no?
Source:
private AdminClient createAdminClient() { final Properties properties = new Properties(); properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers()); return AdminClient.create(properties); }

Target:
method should be static
Source:
public KStream<K, V> repartition(final Repartitioned<K, V> repartitioned) { Objects.requireNonNull(repartitioned, "repartitioned parameter can't be null"); return doRepartition(repartitioned, null); }

Target:
We can do all `null` checks in a single place within `doRepartition()` -- also, all other methods just say ` <parameterName> can't be null` (ie, please remove `parameter` for consistency)
Source:
public boolean rejoinNeededOrPending() { if (!subscriptions.partitionsAutoAssigned()) return false; // we need to rejoin if we performed the assignment and metadata has changed if (assignmentSnapshot != null && !assignmentSnapshot.matches(metadataSnapshot)) { maybeLosePartitions("topic metadata has changed and therefore some topics may not exist any moreq"); return true; } // we need to join if our subscription has changed since the last join if (joinedSubscription != null && !joinedSubscription.equals(subscriptions.subscription())) { maybeLosePartitions("consumer subscription has changed since last joined generation"); return true; } return super.rejoinNeededOrPending(); }

Target:
typo: moreq -> more
Source:
public void close(Duration timeout) { if (timeout.toMillis() < 0) throw new IllegalArgumentException("The timeout cannot be negative."); acquire(); try { if (!closed) { close(timeout.toMillis(), false); closed = true; } } finally { release(); } }

Target:
What was the motivation to change this?
Source:
private Topology createConnectedStateStoreTopology(final String storeName) { final StoreBuilder<KeyValueStore<String, String>> storeBuilder = Stores.keyValueStoreBuilder(Stores.inMemoryKeyValueStore(storeName), Serdes.String(), Serdes.String()); return topology .addSource("source", STRING_DESERIALIZER, STRING_DESERIALIZER, INPUT_TOPIC_1) .addProcessor("processor", defineWithStores(new StatefulProcessor(storeName), Collections.singleton(storeBuilder)), "source") .addSink("counts", OUTPUT_TOPIC_1, "processor"); }

Target:
nit: line too long
Source:
protected DefaultRecord(int sizeInBytes, byte attributes, long offset, long timestamp, int sequence, ByteBuffer key, ByteBuffer value, Header[] headers) { this.sizeInBytes = sizeInBytes; this.attributes = attributes; this.offset = offset; this.timestamp = timestamp; this.sequence = sequence; this.key = key; this.value = value; this.headers = headers; }

Target:
Default access is probably good enough and is consistent with `PartialDefaultRecord`.