keith-turner commented on code in PR #3955:
URL: https://github.com/apache/accumulo/pull/3955#discussion_r1399878926


##########
server/base/src/main/java/org/apache/accumulo/server/util/FindCompactionTmpFiles.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.server.util;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.accumulo.core.metadata.schema.Ample.DataLevel;
+import org.apache.accumulo.core.metadata.schema.ExternalCompactionId;
+import org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType;
+import org.apache.accumulo.core.trace.TraceUtil;
+import org.apache.accumulo.core.util.UtilWaitThread;
+import org.apache.accumulo.core.volume.Volume;
+import org.apache.accumulo.server.ServerContext;
+import org.apache.accumulo.server.cli.ServerUtilOpts;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.Path;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.beust.jcommander.Parameter;
+
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.context.Scope;
+
+public class FindCompactionTmpFiles {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(FindCompactionTmpFiles.class);
+
+  static class Opts extends ServerUtilOpts {
+
+    @Parameter(names = "--tables", description = "comma separated list of 
table names")
+    String tables;
+
+    @Parameter(names = "--delete", description = "if true, will delete tmp 
files")
+    boolean delete = false;
+  }
+
+  public static List<Path> findTempFiles(ServerContext context, String tableId)
+      throws InterruptedException {
+    String tablePattern = tableId != null ? tableId : "*";
+    final String pattern = "/tables/" + tablePattern + "/*/*";
+    final Collection<Volume> vols = context.getVolumeManager().getVolumes();
+    final ExecutorService svc = Executors.newFixedThreadPool(vols.size());
+    final List<Path> matches = new ArrayList<>(1024);
+    final List<Future<Void>> futures = new ArrayList<>(vols.size());
+    for (Volume vol : vols) {
+      final Path volPattern = new Path(vol.getBasePath() + pattern);
+      LOG.info("Looking for tmp files that match pattern: {}", volPattern);
+      futures.add(svc.submit(() -> {
+        try {
+          FileStatus[] files = vol.getFileSystem().globStatus(volPattern,
+              (p) -> p.getName().contains("_tmp_" + 
ExternalCompactionId.PREFIX));
+          Arrays.stream(files).forEach(fs -> matches.add(fs.getPath()));
+        } catch (IOException e) {
+          LOG.error("Error looking for tmp files in volume: {}", vol, e);
+        }
+        return null;
+      }));
+    }
+    svc.shutdown();
+
+    while (futures.size() > 0) {
+      UtilWaitThread.sleep(10_000);
+      Iterator<Future<Void>> iter = futures.iterator();
+      while (iter.hasNext()) {
+        Future<Void> future = iter.next();
+        if (future.isDone()) {
+          iter.remove();
+          try {
+            future.get();
+          } catch (InterruptedException | ExecutionException e) {
+            throw new RuntimeException("Error getting list of tmp files", e);
+          }
+        }
+      }
+    }
+    svc.awaitTermination(10, TimeUnit.MINUTES);
+    LOG.debug("Found compaction tmp files: {}", matches);
+
+    // Remove paths of all active external compaction output files from the 
set of
+    // tmp files found on the filesystem. This must be done *after* gathering 
the
+    // matches on the filesystem.
+    for (DataLevel level : DataLevel.values()) {
+      
context.getAmple().readTablets().forLevel(level).fetch(ColumnType.ECOMP).build()
+          .forEach(tm -> {
+            tm.getExternalCompactions().values()
+                .forEach(ecm -> 
matches.remove(ecm.getCompactTmpName().getPath()));

Review Comment:
   Matches is a list, could make it a set since remove is called on it here.



##########
server/base/src/main/java/org/apache/accumulo/server/util/FindCompactionTmpFiles.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.server.util;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.accumulo.core.metadata.schema.Ample.DataLevel;
+import org.apache.accumulo.core.metadata.schema.ExternalCompactionId;
+import org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType;
+import org.apache.accumulo.core.trace.TraceUtil;
+import org.apache.accumulo.core.util.UtilWaitThread;
+import org.apache.accumulo.core.volume.Volume;
+import org.apache.accumulo.server.ServerContext;
+import org.apache.accumulo.server.cli.ServerUtilOpts;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.Path;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.beust.jcommander.Parameter;
+
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.context.Scope;
+
+public class FindCompactionTmpFiles {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(FindCompactionTmpFiles.class);
+
+  static class Opts extends ServerUtilOpts {
+
+    @Parameter(names = "--tables", description = "comma separated list of 
table names")
+    String tables;
+
+    @Parameter(names = "--delete", description = "if true, will delete tmp 
files")
+    boolean delete = false;
+  }
+
+  public static List<Path> findTempFiles(ServerContext context, String tableId)
+      throws InterruptedException {
+    String tablePattern = tableId != null ? tableId : "*";
+    final String pattern = "/tables/" + tablePattern + "/*/*";
+    final Collection<Volume> vols = context.getVolumeManager().getVolumes();
+    final ExecutorService svc = Executors.newFixedThreadPool(vols.size());
+    final List<Path> matches = new ArrayList<>(1024);

Review Comment:
   Based on other comment about this variable, could change it to the following.
   
   ```suggestion
       final Set<Path> matches = ConcurrentHashMap.newKeySet()
   ```



##########
test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompaction_2_IT.java:
##########
@@ -130,6 +136,10 @@ public void testSplitCancelsExternalCompaction() throws 
Exception {
       // compaction above in the test. Even though the external compaction was 
cancelled
       // because we split the table, FaTE will continue to queue up a 
compaction
       client.tableOperations().cancelCompaction(table1);
+
+      // Verify that the tmp file are cleaned up
+      Wait.waitFor(() -> FindCompactionTmpFiles
+          .findTempFiles(getCluster().getServerContext(), 
tid.canonical()).size() == 1);

Review Comment:
   Should this wait for zero?
   
   ```suggestion
             .findTempFiles(getCluster().getServerContext(), 
tid.canonical()).size() == 0);
   ```



##########
server/base/src/main/java/org/apache/accumulo/server/util/FindCompactionTmpFiles.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.server.util;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.accumulo.core.metadata.schema.Ample.DataLevel;
+import org.apache.accumulo.core.metadata.schema.ExternalCompactionId;
+import org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType;
+import org.apache.accumulo.core.trace.TraceUtil;
+import org.apache.accumulo.core.util.UtilWaitThread;
+import org.apache.accumulo.core.volume.Volume;
+import org.apache.accumulo.server.ServerContext;
+import org.apache.accumulo.server.cli.ServerUtilOpts;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.Path;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.beust.jcommander.Parameter;
+
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.context.Scope;
+
+public class FindCompactionTmpFiles {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(FindCompactionTmpFiles.class);
+
+  static class Opts extends ServerUtilOpts {
+
+    @Parameter(names = "--tables", description = "comma separated list of 
table names")
+    String tables;
+
+    @Parameter(names = "--delete", description = "if true, will delete tmp 
files")
+    boolean delete = false;
+  }
+
+  public static List<Path> findTempFiles(ServerContext context, String tableId)
+      throws InterruptedException {
+    String tablePattern = tableId != null ? tableId : "*";
+    final String pattern = "/tables/" + tablePattern + "/*/*";
+    final Collection<Volume> vols = context.getVolumeManager().getVolumes();
+    final ExecutorService svc = Executors.newFixedThreadPool(vols.size());
+    final List<Path> matches = new ArrayList<>(1024);
+    final List<Future<Void>> futures = new ArrayList<>(vols.size());
+    for (Volume vol : vols) {
+      final Path volPattern = new Path(vol.getBasePath() + pattern);
+      LOG.info("Looking for tmp files that match pattern: {}", volPattern);
+      futures.add(svc.submit(() -> {
+        try {
+          FileStatus[] files = vol.getFileSystem().globStatus(volPattern,
+              (p) -> p.getName().contains("_tmp_" + 
ExternalCompactionId.PREFIX));
+          Arrays.stream(files).forEach(fs -> matches.add(fs.getPath()));
+        } catch (IOException e) {
+          LOG.error("Error looking for tmp files in volume: {}", vol, e);
+        }
+        return null;
+      }));
+    }
+    svc.shutdown();
+
+    while (futures.size() > 0) {
+      UtilWaitThread.sleep(10_000);
+      Iterator<Future<Void>> iter = futures.iterator();
+      while (iter.hasNext()) {
+        Future<Void> future = iter.next();
+        if (future.isDone()) {
+          iter.remove();
+          try {
+            future.get();
+          } catch (InterruptedException | ExecutionException e) {
+            throw new RuntimeException("Error getting list of tmp files", e);
+          }
+        }
+      }
+    }
+    svc.awaitTermination(10, TimeUnit.MINUTES);
+    LOG.debug("Found compaction tmp files: {}", matches);
+
+    // Remove paths of all active external compaction output files from the 
set of
+    // tmp files found on the filesystem. This must be done *after* gathering 
the
+    // matches on the filesystem.
+    for (DataLevel level : DataLevel.values()) {
+      
context.getAmple().readTablets().forLevel(level).fetch(ColumnType.ECOMP).build()
+          .forEach(tm -> {
+            tm.getExternalCompactions().values()
+                .forEach(ecm -> 
matches.remove(ecm.getCompactTmpName().getPath()));
+          });
+    }
+    LOG.debug("Final set of compaction tmp files after removing active 
compactions: {}", matches);
+    return matches;
+  }
+
+  public static class DeleteStats {
+    public int success = 0;
+    public int failure = 0;
+    public int error = 0;
+  }
+
+  public static DeleteStats deleteTempFiles(ServerContext context, List<Path> 
filesToDelete)
+      throws InterruptedException {
+
+    final ExecutorService delSvc = Executors.newFixedThreadPool(8);
+    final List<Future<Boolean>> futures = new 
ArrayList<>(filesToDelete.size());
+    final DeleteStats stats = new DeleteStats();
+
+    filesToDelete.forEach(p -> {
+      futures.add(delSvc.submit(() -> context.getVolumeManager().delete(p)));

Review Comment:
   Will this throw an exception when the files does not exists?  This util may 
find files that the system was in the process of renaming or deleting anyway, 
would be nice to only show errors when the file does not exists.



##########
server/base/src/main/java/org/apache/accumulo/server/util/FindCompactionTmpFiles.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.server.util;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.accumulo.core.metadata.schema.Ample.DataLevel;
+import org.apache.accumulo.core.metadata.schema.ExternalCompactionId;
+import org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType;
+import org.apache.accumulo.core.trace.TraceUtil;
+import org.apache.accumulo.core.util.UtilWaitThread;
+import org.apache.accumulo.core.volume.Volume;
+import org.apache.accumulo.server.ServerContext;
+import org.apache.accumulo.server.cli.ServerUtilOpts;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.Path;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.beust.jcommander.Parameter;
+
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.context.Scope;
+
+public class FindCompactionTmpFiles {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(FindCompactionTmpFiles.class);
+
+  static class Opts extends ServerUtilOpts {
+
+    @Parameter(names = "--tables", description = "comma separated list of 
table names")
+    String tables;
+
+    @Parameter(names = "--delete", description = "if true, will delete tmp 
files")
+    boolean delete = false;
+  }
+
+  public static List<Path> findTempFiles(ServerContext context, String tableId)
+      throws InterruptedException {
+    String tablePattern = tableId != null ? tableId : "*";
+    final String pattern = "/tables/" + tablePattern + "/*/*";
+    final Collection<Volume> vols = context.getVolumeManager().getVolumes();
+    final ExecutorService svc = Executors.newFixedThreadPool(vols.size());
+    final List<Path> matches = new ArrayList<>(1024);
+    final List<Future<Void>> futures = new ArrayList<>(vols.size());
+    for (Volume vol : vols) {
+      final Path volPattern = new Path(vol.getBasePath() + pattern);
+      LOG.info("Looking for tmp files that match pattern: {}", volPattern);
+      futures.add(svc.submit(() -> {
+        try {
+          FileStatus[] files = vol.getFileSystem().globStatus(volPattern,
+              (p) -> p.getName().contains("_tmp_" + 
ExternalCompactionId.PREFIX));
+          Arrays.stream(files).forEach(fs -> matches.add(fs.getPath()));

Review Comment:
   Multiple threads are adding to matches which is not a concurrent or 
synchronized collection.



-- 
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]

Reply via email to