szetszwo commented on code in PR #10171: URL: https://github.com/apache/ozone/pull/10171#discussion_r3836500264
########## hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/TableCacheUpdateTracker.java: ########## @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hdds.utils.db; + +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * Tracks table caches updated by the current thread. + */ +public final class TableCacheUpdateTracker { + + private static final ThreadLocal<Tracker> CURRENT_TRACKER = + new ThreadLocal<>(); + private static final Set<String> TRACKING = Collections.emptySet(); Review Comment: Since Collections.emptySet() is immutable, TRACKING is always an empty set. Let's remove it and just use null (i.e. comparing "tables == null" instead of "table == TRACKING".) ########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/response/OMClientResponse.java: ########## @@ -86,5 +91,28 @@ public void setOmLockDetails( OMLockDetails omLockDetails) { this.omLockDetails = omLockDetails; } -} + public Set<String> getCleanupTables() { + return cleanupTables; + } + + public void setCleanupTables(Collection<String> tables) { + cleanupTables = toCleanupTables(tables); + } + + public void addCleanupTables(Collection<String> tables) { + if (tables == null || tables.isEmpty()) { + return; + } + Set<String> merged = new LinkedHashSet<>(cleanupTables); + merged.addAll(tables); + cleanupTables = toCleanupTables(merged); Review Comment: merged is already a new set. Just make it unmodifiable: ```java cleanupTables = Collections.unmodifiableSet(merged); ``` ########## hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/TableCacheUpdateTracker.java: ########## @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hdds.utils.db; + +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * Tracks table caches updated by the current thread. + */ +public final class TableCacheUpdateTracker { + + private static final ThreadLocal<Tracker> CURRENT_TRACKER = + new ThreadLocal<>(); + private static final Set<String> TRACKING = Collections.emptySet(); + + private TableCacheUpdateTracker() { + } + + public static Tracker track() { + Tracker tracker = new Tracker(CURRENT_TRACKER.get()); + CURRENT_TRACKER.set(tracker); + return tracker; + } + + public static void recordCacheUpdate(String tableName) { + Tracker tracker = CURRENT_TRACKER.get(); + if (tracker != null) { + tracker.recordCacheUpdate(tableName); + } + } + + /** + * Tracks updated tables until the scope is closed. + */ + public static final class Tracker implements AutoCloseable { Review Comment: Inner class is not needed -- just move everything to TableCacheUpdateTracker. ########## hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/TableCacheUpdateTracker.java: ########## @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hdds.utils.db; Review Comment: Let's put it to org.apache.hadoop.hdds.utils.db.cache? ########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/response/OMClientResponse.java: ########## @@ -86,5 +91,28 @@ public void setOmLockDetails( OMLockDetails omLockDetails) { this.omLockDetails = omLockDetails; } -} + public Set<String> getCleanupTables() { + return cleanupTables; + } + + public void setCleanupTables(Collection<String> tables) { + cleanupTables = toCleanupTables(tables); + } Review Comment: This is only used for testing. Let's change it as below: ```java public void setCleanupTablesForTesting(Collection<String> tables) { cleanupTables = tables == null || tables.isEmpty() ? Collections.emptySet() : Collections.unmodifiableSet(new LinkedHashSet<>(tables)); } ``` ########## hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/TableCacheUpdateTracker.java: ########## @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hdds.utils.db; + +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * Tracks table caches updated by the current thread. + */ +public final class TableCacheUpdateTracker { + + private static final ThreadLocal<Tracker> CURRENT_TRACKER = + new ThreadLocal<>(); + private static final Set<String> TRACKING = Collections.emptySet(); + + private TableCacheUpdateTracker() { + } + + public static Tracker track() { + Tracker tracker = new Tracker(CURRENT_TRACKER.get()); + CURRENT_TRACKER.set(tracker); + return tracker; + } + + public static void recordCacheUpdate(String tableName) { + Tracker tracker = CURRENT_TRACKER.get(); + if (tracker != null) { + tracker.recordCacheUpdate(tableName); + } + } + + /** + * Tracks updated tables until the scope is closed. + */ + public static final class Tracker implements AutoCloseable { + private final Tracker parent; Review Comment: Add a thread field and assertion: ```java private final Thread thread = Thread.currentThread(); ``` ```java //for all non-static methods, add Preconditions.assertSame(thread, Thread.currentThread(), "thread"); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
