auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
START public void setFieldNames(String header) throws FormattedException END { try { setFieldNames(splitter.split(header)); } catch (Exception e) { Throwables.propagateIfInstanceOf(e, FormattedException.class); throw new FormattedException.Builder() .withErrorCode(FormattedException.ErrorCode.UNPARSABLE_HEADER) .withMessage(e.getMessage()) .build(); } } FormattedException is a runtime exception, so you don't need to declare it.

Target:
public void setFieldNames(String header) { try { setFieldNames(splitter.split(header)); } catch (Exception e) { Throwables.propagateIfInstanceOf(e, FormattedException.class); throw new FormattedException.Builder() .withErrorCode(FormattedException.ErrorCode.UNPARSABLE_HEADER) .withMessage(e.getMessage()) .build(); } }
Source:
START public Optional<Collection<VirtualGatewayInterface>> getGatewayInterfaces(VirtualGatewayInstance gateway) { END return l3manager.getGatewayInterfaces(gateway); } You want to avoid returning an optional collection. Just return an empty collection

Target:
public Collection<VirtualGatewayInterface> getGatewayInterfaces(VirtualGatewayInstance gateway) { return l3manager.getGatewayInterfaces(gateway); }
Source:
public void enableFirewall(boolean enabled) { if(this.enabled != enabled) { logger.info("Setting firewall to {}", enabled); this.enabled = enabled; List<Masked<U64>> changes = new ArrayList<Masked<U64>>(); START changes.add(Masked.of(DEFAULT_COOKIE, AppCookie.getAppFieldMask())); END // Add announcement that all firewall decisions changed routingService.handleRoutingDecisionChange(changes); } } Can we use an ImmutableSet instead? And is list necessary instead of set?

Target:
public void enableFirewall(boolean enabled) { if(this.enabled != enabled) { logger.info("Setting firewall to {}", enabled); this.enabled = enabled; List<Masked<U64>> changes = ImmutableList.of( Masked.of(DEFAULT_COOKIE, AppCookie.getAppFieldMask()) ); // Add announcement that all firewall decisions changed routingService.handleRoutingDecisionChange(changes); } }
Source:
public void destroyNode(String id) { // stop it first try { suspendNode(id); // wait until fully stopped serverStopped.apply(id); } catch (Exception e) { // ignore START e.printStackTrace(); END } api.getVirtualServerApi().destroy(id); // wait until fully destroyed String systemId = toSystemId.apply(id); systemNormal.apply(systemId); } You'd better use a logger here to ignore the exception or propagate the exception using Throwables.propagate

Target:
public void destroyNode(String id) { // stop it first suspendNode(id); // wait until fully stopped serverStopped.apply(id); api.getVirtualServerApi().destroy(id); // wait until fully destroyed String systemId = toSystemId.apply(id); systemNormal.apply(systemId); }
Source:
public synchronized void manage(ManagedBean mBean, String name) { START if (mBeanServer != null) { END ManagementUtils.register(mBeanServer, mBean, mBean.getType(), name); } mbeans.put(new Key(mBean, name), mBean); } [curiosity] Is it expected that beans are registered when an `mBeanServer` is not set yet?

Target:
public synchronized void manage(ManagedBean mBean, String name) { if (mBeanServer.isPresent()) { ManagementUtils.register(mBeanServer.get(), mBean, mBean.getType(), name); } mbeans.put(new Key(mBean, name), mBean); }
Source:
public LocalizedString getDisplayNames() { if (displayName == null) { String simple = context.getState().getLabel(); if (simple != null) { START displayName = resolveExpression(simple); END } else if (context.getId() != null) { Map<Locale, Described.State> descriptions = navigation.loadDescriptions(context.getId()); displayName = ObjectFactory.createLocalizedString(descriptions); } } return displayName; } There will be slight perf cost now that you are going to look through all resource bundles and resolve all languages when someone calls getDisplayNames(). Again I'm not sure about the use case as to why this should be done. getDisplayName() properly resolves the label.

Target:
public LocalizedString getDisplayNames() { if (displayName == null) { String simple = context.getState().getLabel(); if (simple != null) { displayName = navigation.resolveExpression(simple); } else if (context.getId() != null) { Map<Locale, Described.State> descriptions = navigation.loadDescriptions(context.getId()); displayName = ObjectFactory.createLocalizedString(descriptions); } } return displayName; }
Source:
START Properties getProperties() { END return props; } 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?

Target:
public Properties getProperties() { return props; }
Source:
START public synchronized void removeDomain( final String domainId ) { END delegate.removeDomain( domainId ); purgeDomain( domainId ); removeDomainFromIDCache( domainId ); } 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 ?

Target:
public void removeDomain( final String domainId ) { delegate.removeDomain( domainId ); purgeDomain( domainId ); removeDomainFromIDCache( domainId ); }
Source:
private void placeBreadcrumbFile( String folderPath, boolean success ) { String ext = ERROR; if ( success ) { ext = OK; } String breadcrumbFilename = folderPath + WI_STATUS + ext; START File breadcrumb = new File( breadcrumbFilename ); END 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" ) ); } } use File object to construct the breadcrumb file: new File( folderPath, WI_STATUS + etx);

Target:
private void placeBreadcrumbFile( String folderPath, boolean success ) { String ext = ERROR; if ( success ) { ext = OK; } File breadcrumb = new File( folderPath, WI_STATUS + ext ); 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" ) ); } }
Source:
public boolean visit(MethodDeclaration node) { if (isMethod(node, "android.widget.TextView", "onDraw", "android.graphics.Canvas")) { final ASTBuilder b = this.ctx.getASTBuilder(); START final Refactorings r = this.ctx.getRefactorings(); END node.accept(new OnDrawTransformer(this.ctx, node)); } return VISIT_SUBTREE; } Please remove unused `b` and `r`

Target:
public boolean visit(MethodDeclaration node) { if (isMethod(node, "android.widget.TextView", "onDraw", "android.graphics.Canvas")) { node.accept(new OnDrawTransformer(this.ctx, node)); } return VISIT_SUBTREE; }
Source:
public boolean visit(Assignment node) { START if (ASTHelper.isSameLocalVariable(node.getLeftHandSide(), cursorSimpleName)) { END return DO_NOT_VISIT_SUBTREE; } return VISIT_SUBTREE; } Please change all uses of `ASTHelper` methods and fields to use static imports.

Target:
public boolean visit(Assignment node) { if (isSameLocalVariable(node.getLeftHandSide(), cursorSimpleName)) { return DO_NOT_VISIT_SUBTREE; } return VISIT_SUBTREE; }
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); START cursor.close(); END } 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.

Target:
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); if (cursor != null) { cursor.close(); } }
Source:
public String replaceToStringCallOnCurrentObjectInStringConcat() { String s1 = " " + StringSample.this + StringSample.this + this; START String s2 = StringSample.this + " " + this; END return s1 + s2; } 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()`.

Target:
public String replaceToStringCallOnCurrentObjectInStringConcat() { String s1 = String.valueOf(StringSample.this) + StringSample.this + this; String s2 = String.valueOf(StringSample.this) + this; return s1 + s2; }
Source:
public Boolean doNotThrowAnyException(boolean bo) { class ClassWithBooleanField { Boolean b; } ClassWithBooleanField objWithBooleanField = new ClassWithBooleanField(); Boolean aBoolean = Boolean.TRUE; START return bo ? objWithBooleanField.b : aBoolean; END } Please change the code to match how it is now replaced. I just wanted to be sure that there was no exception thrown here.

Target:
public Boolean doNotThrowAnyException(boolean bo) { class ClassWithBooleanField { Boolean b; } ClassWithBooleanField objWithBooleanField = new ClassWithBooleanField(); return bo ? objWithBooleanField.b : Boolean.TRUE; }
Source:
public static boolean isMethod(MethodInvocation node, String typeQualifiedName, START String methodName, String... parameterTypesQualifiedNames) { END if (node == null) { return false; } final IMethodBinding methodBinding = node.resolveMethodBinding(); return isMethod(methodBinding, typeQualifiedName, methodName, parameterTypesQualifiedNames); } 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.

Target:
public static boolean isMethod(MethodInvocation node, String typeQualifiedName, String methodName, String... parameterTypesQualifiedNames) { return node != null && isMethod(node.resolveMethodBinding(), typeQualifiedName, methodName, parameterTypesQualifiedNames); }
Source:
private void updatePositionLeaderNoValidation(Metadata.LeaderAndEpoch currentLeaderAndEpoch) { if (position != null && !position.currentLeader.equals(currentLeaderAndEpoch)) { START FetchPosition newPosition = new FetchPosition(position.offset, position.offsetEpoch, currentLeaderAndEpoch); END transitionState(FetchStates.FETCHING, () -> { this.position = newPosition; this.nextRetryTimeMs = null; }); } } nit: ```newPosition``` can be created lazy.

Target:
private void updatePositionLeaderNoValidation(Metadata.LeaderAndEpoch currentLeaderAndEpoch) { if (position != null) { FetchPosition newPosition = new FetchPosition(position.offset, position.offsetEpoch, currentLeaderAndEpoch); transitionState(FetchStates.FETCHING, () -> { this.position = newPosition; this.nextRetryTimeMs = null; }); } }
Source:
private Map<TopicPartition, Long> checkpointableOffsets() { START if (state() == State.RESTORING) { END 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; } Should we return emptyMap if we are SUSPENDED as well?

Target:
private Map<TopicPartition, Long> checkpointableOffsets() { 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; }
Source:
private Map<TopicPartition, Long> committedOffsetForChangelogs(final Set<TopicPartition> partitions) { final Map<TopicPartition, Long> committedOffsets; try { committedOffsets = fetchCommittedOffsets(partitions, mainConsumer); } catch (final StreamsException e) { START if (e.getCause() instanceof TimeoutException) { END // if it timed out we just retry next time return Collections.emptyMap(); } else { throw e; } } lastUpdateOffsetTime = time.milliseconds(); return committedOffsets; } This seems to be a step backwards, actually. Why wrap it as a StreamsException only just to immediately unwrap it again?

Target:
private Map<TopicPartition, Long> committedOffsetForChangelogs(final Set<TopicPartition> partitions) { final Map<TopicPartition, Long> committedOffsets; try { committedOffsets = fetchCommittedOffsets(partitions, mainConsumer); } catch (final TimeoutException e) { // if it timed out we just retry next time return Collections.emptyMap(); } lastUpdateOffsetTime = time.milliseconds(); return committedOffsets; }
Source:
public void fetchEndOffsetsShouldRethrowRuntimeExceptionAsStreamsException() { final Admin adminClient = EasyMock.createMock(AdminClient.class); EasyMock.expect(adminClient.listOffsets(EasyMock.anyObject())).andThrow(new RuntimeException()); replay(adminClient); START assertThrows(StreamsException.class, () -> fetchEndOffsets(emptyList(), adminClient, 60_000L)); END verify(adminClient); } Should we pass in `MAX_VALUE` to avoid introducing test flakyness?

Target:
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)); verify(adminClient); }
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; START this.dlqTopicName = connConfig.dlqTopicName(); END } Should we trim this? ```suggestion this.dlqTopicName = connConfig.dlqTopicName().trim(); ```

Target:
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().trim(); }
Source:
START public InternalSinkRecord(ConsumerRecord<byte[], byte[]> originalRecord, String topic, END 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; } Let's make this protected. ```suggestion protected InternalSinkRecord(ConsumerRecord<byte[], byte[]> originalRecord, String topic, ```

Target:
protected 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; }
Source:
public void awaitAllFutures() { START Future<?> future = null; END 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); } } } nit: initialization is not required

Target:
public void awaitAllFutures() { Future<?> future; 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); } } }
Source:
public boolean test(R record) { START return record.key() == null; END } I think we want to check the value instead of the key here? ```suggestion return record.value() == null; ```

Target:
public boolean test(R record) { return record.value() == null; }
Source:
public JmxReporter(String prefix) { START Objects.requireNonNull(prefix); END this.prefix = prefix; } Should we throw an exception here, or just replace the null value with an empty string?

Target:
public JmxReporter(String prefix) { this.prefix = prefix != null ? prefix : ""; }
Source:
public boolean equals(Object other) { if (this == other) { return true; } START if (other == null || !(other instanceof Features)) { END return false; } final Features that = (Features) other; return Objects.equals(this.features, that.features); } we don't need to check `other == null` here, the next condition check covers it.

Target:
public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof Features)) { return false; } final Features that = (Features) other; return Objects.equals(this.features, that.features); }
Source:
public NewTopicBuilder partitions(int numPartitions) { START if (numPartitions == NO_PARTITIONS) { END return defaultPartitions(); } this.numPartitions = numPartitions; return this; } 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?

Target:
public NewTopicBuilder partitions(int numPartitions) { this.numPartitions = numPartitions; return this; }
Source:
public NewTopicBuilder replicationFactor(short replicationFactor) { START if (replicationFactor == NO_REPLICATION_FACTOR) { END return defaultReplicationFactor(); } this.replicationFactor = replicationFactor; return this; } same question as above

Target:
public NewTopicBuilder replicationFactor(short replicationFactor) { this.replicationFactor = replicationFactor; return this; }
Source:
private void waitForRunning(final List<KeyValue<KafkaStreams.State, KafkaStreams.State>> observed) throws Exception { waitForCondition( START () -> observed.get(observed.size() - 1).equals(new KeyValue<>(State.REBALANCING, State.RUNNING)), END MAX_WAIT_TIME_MS, () -> "Client did not startup on time. Observers transitions: " + observed ); } 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?

Target:
private void waitForRunning(final List<KeyValue<KafkaStreams.State, KafkaStreams.State>> observed) throws Exception { waitForCondition( () -> !observed.isEmpty() && observed.get(observed.size() - 1).value.equals(State.RUNNING), MAX_WAIT_TIME_MS, () -> "Client did not startup on time. Observers transitions: " + observed ); }
Source:
protected void configureHttpResponsHeaderFilter(ServletContextHandler context) { String headerConfig = config.getString(WorkerConfig.RESPONSE_HTTP_HEADERS_CONFIG); START log.debug("headerConfig : " + headerConfig); END 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)); } 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?

Target:
protected void configureHttpResponsHeaderFilter(ServletContextHandler context) { String headerConfig = config.getString(WorkerConfig.RESPONSE_HTTP_HEADERS_CONFIG); FilterHolder headerFilterHolder = new FilterHolder(HeaderFilter.class); headerFilterHolder.setInitParameter("headerConfig", headerConfig); context.addFilter(headerFilterHolder, "/*", EnumSet.of(DispatcherType.REQUEST)); }
Source:
public RemoveMembersFromConsumerGroupOptions() { START this.members = Collections.emptySet();; END } nit: extra semi-colon

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

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

Target:
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 ); } }
Source:
public void shouldWarnAboutRocksDBConfigSetterIsNotGuaranteedToBeBackwardsCompatible() { props.setProperty(StreamsConfig.ROCKSDB_CONFIG_SETTER_CLASS_CONFIG, TestRocksDbConfigSetter.class.getName()); START final LogCaptureAppender appender = LogCaptureAppender.createAndRegister(); END 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`).")); } Seems like this appender could also be scoped to the class that logs the message we want to capture, right?

Target:
public void shouldWarnAboutRocksDBConfigSetterIsNotGuaranteedToBeBackwardsCompatible() { props.setProperty(StreamsConfig.ROCKSDB_CONFIG_SETTER_CLASS_CONFIG, TestRocksDbConfigSetter.class.getName()); try (final LogCaptureAppender appender = LogCaptureAppender.createAndRegister()) { new KafkaStreams(new StreamsBuilder().build(), props, supplier, time); 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`).")); } }
Source:
void offer(final UUID client) { if (uniqueClients.contains(client)) { clientsByTaskLoad.remove(client); } clientsByTaskLoad.offer(client); START uniqueClients.add(client); END } prop: I would not add the client if it is already contained in the set.

Target:
void offer(final UUID client) { if (uniqueClients.contains(client)) { clientsByTaskLoad.remove(client); } else { uniqueClients.add(client); } clientsByTaskLoad.offer(client); }
Source:
public void close() { START 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; } END } Here's a nice blog re: exception costs and when they occur: https://shipilev.net/blog/2014/exceptional-performance

Target:
public void close() { ConnectException e = null; for (ErrorReporter reporter : reporters) { try { reporter.close(); } catch (Throwable t) { e = e != null ? e : new ConnectException("Failed to close all reporters"); e.addSuppressed(t); } } if (e != null) { throw e; } }
Source:
static void throwSuppressed(final String message, final LinkedList<RuntimeException> suppressed) { if (suppressed != null && !suppressed.isEmpty()) { final RuntimeException firstCause = suppressed.pollFirst(); START final RuntimeException toThrow = new RuntimeException(message, firstCause); END for (final RuntimeException e : suppressed) { toThrow.addSuppressed(e); } throw toThrow; } } The first exception will be the "cause"

Target:
static void throwSuppressed(final String message, final LinkedList<RuntimeException> suppressed) { if (!suppressed.isEmpty()) { final RuntimeException firstCause = suppressed.pollFirst(); final RuntimeException toThrow = new RuntimeException(message, firstCause); for (final RuntimeException e : suppressed) { toThrow.addSuppressed(e); } throw toThrow; } }
Source:
static LinkedList<RuntimeException> executeAll(final Runnable... actions) { START LinkedList<RuntimeException> suppressed = null; END for (final Runnable action : actions) { try { action.run(); } catch (final RuntimeException exception) { if (suppressed == null) { suppressed = new LinkedList<>(); } suppressed.add(exception); } } return suppressed; } Why not make it `final` and return an empty list? It seems better to avoid using `null` if possible

Target:
static LinkedList<RuntimeException> executeAll(final Runnable... actions) { final LinkedList<RuntimeException> suppressed = new LinkedList<>(); for (final Runnable action : actions) { try { action.run(); } catch (final RuntimeException exception) { suppressed.add(exception); } } return suppressed; }
Source:
public void close() { lock.writeLock().lock(); try { final LinkedList<RuntimeException> suppressed = executeAll( () -> cache.flush(cacheName), () -> cache.close(cacheName), wrapped()::close ); START if (suppressed != null && !suppressed.isEmpty()) { END throwSuppressed("Caught an exception while closing caching key value store for store " + name(), suppressed); } } finally { lock.writeLock().unlock(); } } The check itself (ie, for `isEmpty()` makes sense) but I would still try to avoid the `null`

Target:
public void close() { lock.writeLock().lock(); try { final LinkedList<RuntimeException> suppressed = executeAll( () -> cache.flush(cacheName), () -> cache.close(cacheName), wrapped()::close ); if (!suppressed.isEmpty()) { throwSuppressed("Caught an exception while closing caching key value store for store " + name(), suppressed); } } finally { lock.writeLock().unlock(); } }
Source:
private boolean shouldUsePreviousAssignment() { if (previousAssignmentIsValid()) { final int previousAssignmentBalanceFactor = START computeBalanceFactor(clientStates.values(), statefulTasks, ClientState::prevActiveTasks); END return previousAssignmentBalanceFactor <= configs.balanceFactor; } else { return false; } } Q: Why do we only consider stateful tasks in `computeBalanceFactor()`?

Target:
private boolean shouldUsePreviousAssignment() { if (previousAssignmentIsValid()) { final int previousAssignmentBalanceFactor = computeBalanceFactor(clientStates.values(), statefulTasks); return previousAssignmentBalanceFactor <= configs.balanceFactor; } else { return false; } }
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"); START this.eosEnabled = processingMode == EXACTLY_ONCE_ALPHA || processingMode == EXACTLY_ONCE_BETA; END log = Objects.requireNonNull(logContext, "logContext cannot be null").logger(getClass()); logPrefix = logContext.logPrefix().trim(); } 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; } ```

Target:
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"); log = Objects.requireNonNull(logContext, "logContext cannot be null").logger(getClass()); logPrefix = logContext.logPrefix().trim(); }
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 START pauseChangelogsFromRestoreConsumer(standbyRestoringChangelogs()); END } state = ChangelogReaderState.ACTIVE_RESTORING; } We can move the line 290 inside as well?

Target:
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; } }
Source:
ClientMetadata(final String endPoint) { // get the host info if possible START hostInfo = endPoint != null ? HostInfo.buildFromEndpoint(endPoint) : null; END // initialize the consumer memberIds consumers = new HashSet<>(); // initialize the client state state = new ClientState(); } nit: I feel we could internalize the hostInfo creation, including the null check: `hostInfo = HostInfo.buildFromEndpoint(endPoint);`

Target:
ClientMetadata(final String endPoint) { // get the host info, or null if no endpoint is configured (ie endPoint == null) hostInfo = HostInfo.buildFromEndpoint(endPoint); // initialize the consumer memberIds consumers = new HashSet<>(); // initialize the client state state = new ClientState(); }
Source:
START private Map<String, List<TaskId>> expectedAssignmentForTwoClients(final List<TaskId> assignedTasksForClient1, END final List<TaskId> assignedTasksForClient2) { return mkMap( mkEntry(CLIENT_1, assignedTasksForClient1), mkEntry(CLIENT_2, assignedTasksForClient2) ); } These utility methods can be static.

Target:
private static Map<String, List<TaskId>> expectedAssignmentForTwoClients(final List<TaskId> assignedTasksForClient1, final List<TaskId> assignedTasksForClient2) { return mkMap( mkEntry(CLIENT_1, assignedTasksForClient1), mkEntry(CLIENT_2, assignedTasksForClient2) ); }
Source:
public RawAndDeserializedValue<V> getWithBinary(final K key) { try { START final byte[] serializedValue = wrapped().get(keyBytes(key)); return new RawAndDeserializedValue<V>(serializedValue, maybeMeasureLatency(() -> outerValue(serializedValue), time, getSensor)); END } catch (final ProcessorStateException e) { final String message = String.format(e.getMessage(), key); throw new ProcessorStateException(message, e); } } 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.

Target:
public RawAndDeserializedValue<V> getWithBinary(final K key) { try { return maybeMeasureLatency(() -> { final byte[] serializedValue = wrapped().get(keyBytes(key)); return new RawAndDeserializedValue<V>(serializedValue, outerValue(serializedValue)); }, time, getSensor); } catch (final ProcessorStateException e) { final String message = String.format(e.getMessage(), key); throw new ProcessorStateException(message, e); } }
Source:
public void shouldGetWithBinary() { expect(inner.get(keyBytes)).andReturn(valueAndTimestampBytes); inner.put(eq(keyBytes), aryEq(valueAndTimestampBytes)); expectLastCall(); init(); START metered.put(key, valueAndTimestamp); END final RawAndDeserializedValue<String> valueWithBinary = metered.getWithBinary(key); assertEquals(valueWithBinary.value, valueAndTimestamp); assertEquals(valueWithBinary.serializedValue, valueAndTimestampBytes); } Since you mocked the inner store `get`, you shouldn't need to actually do a `put`, right?

Target:
public void shouldGetWithBinary() { expect(inner.get(keyBytes)).andReturn(valueAndTimestampBytes); inner.put(eq(keyBytes), aryEq(valueAndTimestampBytes)); expectLastCall(); init(); final RawAndDeserializedValue<String> valueWithBinary = metered.getWithBinary(key); assertEquals(valueWithBinary.value, valueAndTimestamp); assertEquals(valueWithBinary.serializedValue, valueAndTimestampBytes); }
Source:
public void shouldNotPutIfSameValuesAndGreaterTimestamp() { START expectLastCall(); END 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); } ```suggestion ```

Target:
public void shouldNotPutIfSameValuesAndGreaterTimestamp() { 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); }
Source:
public void shouldTryToLockValidTaskDirsAtRebalanceStart() throws IOException { expectLockObtainedFor(taskId01); expectLockFailedFor(taskId10); makeTaskFolders( START "0_1", END "1_0", "dummy" ); replay(stateDirectory); taskManager.handleRebalanceStart(singleton("topic")); verify(stateDirectory); assertThat(taskManager.lockedTaskDirectories(), is(singleton(taskId01))); } 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`.

Target:
public void shouldTryToLockValidTaskDirsAtRebalanceStart() throws IOException { expectLockObtainedFor(taskId01); expectLockFailedFor(taskId10); makeTaskFolders( taskId01.toString(), taskId10.toString(), "dummy" ); replay(stateDirectory); taskManager.handleRebalanceStart(singleton("topic")); verify(stateDirectory); assertThat(taskManager.lockedTaskDirectories(), is(singleton(taskId01))); }
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); START expectLastCall(); END } 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)`

Target:
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); }
Source:
public ConsumerGroupListing(String groupId, boolean isSimpleConsumerGroup, Optional<ConsumerGroupState> state) { Objects.requireNonNull(state); this.groupId = groupId; this.isSimpleConsumerGroup = isSimpleConsumerGroup; START this.state = state; END } nit: usually we would write this is `this.state = requireNonNull(state);`

Target:
public ConsumerGroupListing(String groupId, boolean isSimpleConsumerGroup, Optional<ConsumerGroupState> state) { this.groupId = groupId; this.isSimpleConsumerGroup = isSimpleConsumerGroup; this.state = Objects.requireNonNull(state); }
Source:
public void postCommit() { switch (state()) { case RUNNING: case RESTORING: START commitNeeded = false; END commitRequested = false; if (eosDisabled) { stateMgr.checkpoint(checkpointableOffsets()); } log.info("Committed"); break; default: throw new IllegalStateException("Illegal state " + state() + " while post committing active task " + id); } } 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.

Target:
public void postCommit() { switch (state()) { case RUNNING: commitNeeded = false; commitRequested = false; if (eosDisabled) { stateMgr.checkpoint(checkpointableOffsets()); } log.info("Committed"); break; case RESTORING: commitNeeded = false; commitRequested = false; stateMgr.checkpoint(checkpointableOffsets()); log.info("Committed"); break; default: throw new IllegalStateException("Illegal state " + state() + " while post committing active task " + id); } }
Source:
private static String getMetricsVersion(final Properties config) { START if (config.containsKey(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG)) { return config.getProperty(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG); } return DEFAULT_METRICS_VERSION; END } req: Please use the following if you still need this method. ```suggestion return config.getProperty(StreamsConfig.BUILT_IN_METRICS_VERSION_CONFIG, DEFAULT_METRICS_VERSION); ```

Target:
private static String getMetricsVersion(final Properties config) { 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(); START } catch (final Exception e) { END throw new TaskMigratedException("Error caught during partitions lost", e); } finally { log.info("partitions lost took ms.", time.milliseconds() - start); } } 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.

Target:
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(); } finally { log.info("partitions lost took ms.", time.milliseconds() - start); } }
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")); START client.close(); END } We are calling `close` even though it doesn't do anything just to avoid warnings? Where are the warnings coming from?

Target:
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")); }
Source:
public void shutdown() { START synchronized (this) { END log.info("Scheduled shutdown for {}", this); stopping = true; notify(); } } 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.

Target:
public synchronized void shutdown() { log.info("Scheduled shutdown for {}", this); stopping = true; notify(); }
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); START log.debug("Added partitions to the restore consumer, current assignment is {}", partitions, assignment); END } Does the consumer not log this already within `assign(...)` ?

Target:
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); }
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!" ); } START END } nit: unneeded newline

Target:
private void waitUntilMirrorMakerIsRunning(EmbeddedConnectCluster connectCluster, Set<String> connNames) throws InterruptedException { for (String connector : connNames) { TestUtils.waitForCondition(() -> areConnectorAndTasksRunning(connectCluster, connector), "Timed out trying to verify connector " + connector + " was up!"); } }
Source:
void addMetrics(Collection<KafkaMetric> metrics) { checkLocked(); if (!beanMetrics.isEmpty()) { unregister(); } for (KafkaMetric metric : metrics) { beanMetrics.put(metric.metricName().name(), metric); START } END register(); } Are we assuming that `metrics` is non-empty?

Target:
void addMetrics(Collection<KafkaMetric> metrics) { checkLocked(); if (metrics.isEmpty()) { return; } if (!beanMetrics.isEmpty()) { unregister(); } for (KafkaMetric metric : metrics) { beanMetrics.put(metric.metricName().name(), metric); } register(); }
Source:
START public CreateTopicsResult(Map<String, KafkaFuture<TopicMetadataAndConfig>> futures) { END this.futures = futures; } 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?)

Target:
protected CreateTopicsResult(Map<String, KafkaFuture<TopicMetadataAndConfig>> futures) { this.futures = futures; }
Source:
public void materialize() { this.queryableName = storeName; if (tableProcessorNode != null) { START tableProcessorNode.setStoreNeeded(true); END } } I don't think we need all this, if we delay adding the store to `writeToTopology()`

Target:
public void materialize() { this.queryableName = storeName; }
Source:
private boolean isProtocolNameInconsistent(String protocolName) { START return protocolName != null && !protocolName.equals(generation().protocol); END } Should we change `Generation.protocol` to `protocolName` as well?

Target:
private boolean isProtocolNameInconsistent(String protocolName) { return protocolName != null && !protocolName.equals(generation().protocolName); }
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); START } END return reqs; } Let's remove the brace changes please.

Target:
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; }
Source:
public void shouldThrowOnSendOffsetsToTransactionIfTransactionsNotInitialized() { buildMockProducer(true); START String groupId = null; END producer.sendOffsetsToTransaction(null, groupId); } To ensure that we are really testing the state machine as expected, we should provide a valid groupId. Similarly below.

Target:
public void shouldThrowOnSendOffsetsToTransactionIfTransactionsNotInitialized() { buildMockProducer(true); producer.sendOffsetsToTransaction(null, groupId); }
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; START } END 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`.

Target:
private boolean isFatalException(Errors error) { return error == Errors.TRANSACTIONAL_ID_AUTHORIZATION_FAILED || error == Errors.INVALID_PRODUCER_EPOCH || error == Errors.UNSUPPORTED_FOR_MESSAGE_FORMAT; }
Source:
public boolean hasNext() { START if (this.peek != null) { END return true; } if (!this.iterator.hasNext()) { this.iterator = this.iterable.iterator(); } return this.iterator.hasNext(); } Why change the logic since the answer is always `true`?

Target:
public boolean hasNext() { return true; }