Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDDS-10568. When the ldb command is executed, it is output by line #7467

Merged
merged 8 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,43 @@ void testScanOfPipelinesWhenNoData() throws IOException {
assertEquals("", stderr.toString());
}

@Test
void testScanWithRecordsPerFile() throws IOException {
// Prepare dummy table
prepareTable(KEY_TABLE, false);

String scanDir1 = tempDir.getAbsolutePath() + "/scandir1";
// Prepare scan args
List<String> completeScanArgs1 = new ArrayList<>(Arrays.asList(
"--db", dbStore.getDbLocation().getAbsolutePath(),
"scan",
"--column-family", KEY_TABLE, "--out", scanDir1,
"--max-records-per-file", "2"));

File tmpDir1 = new File(scanDir1);
tmpDir1.deleteOnExit();

int exitCode1 = cmd.execute(completeScanArgs1.toArray(new String[0]));
xichen01 marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(0, exitCode1);
assertTrue(tmpDir1.isDirectory());
assertEquals(3, tmpDir1.listFiles().length);
xichen01 marked this conversation as resolved.
Show resolved Hide resolved

String scanDir2 = tempDir.getAbsolutePath() + "/scandir2";
// Used with parameter '-l'
List<String> completeScanArgs2 = new ArrayList<>(Arrays.asList(
"--db", dbStore.getDbLocation().getAbsolutePath(),
"scan",
"--column-family", KEY_TABLE, "--out", scanDir2,
"--max-records-per-file", "3", "-l", "2"));
File tmpDir2 = new File(scanDir2);
tmpDir2.deleteOnExit();

int exitCode2 = cmd.execute(completeScanArgs2.toArray(new String[0]));
assertEquals(0, exitCode2);
assertTrue(tmpDir2.isDirectory());
assertEquals(1, tmpDir2.listFiles().length);
}

@Test
void testSchemaCommand() throws IOException {
// Prepare dummy table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import picocli.CommandLine;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -172,6 +173,14 @@ public class DBScanner implements Callable<Void>, SubcommandWithParent {
defaultValue = "10")
private int threadCount;

@CommandLine.Option(names = {"--max-records-per-file"},
description = "The number of records to print per file.",
defaultValue = "0")
private long recordsPerFile;

private int fileSuffix = 0;
private long globalCount = 0;

private static final String KEY_SEPARATOR_SCHEMA_V3 =
new OzoneConfiguration().getObject(DatanodeConfiguration.class)
.getContainerSchemaV3KeySeparator();
Expand All @@ -180,7 +189,8 @@ public class DBScanner implements Callable<Void>, SubcommandWithParent {

@Override
public Void call() throws Exception {

fileSuffix = 0;
globalCount = 0;
List<ColumnFamilyDescriptor> cfDescList =
RocksDBUtils.getColumnFamilyDescriptors(parent.getDbPath());
final List<ColumnFamilyHandle> cfHandleList = new ArrayList<>();
Expand Down Expand Up @@ -240,11 +250,28 @@ private boolean displayTable(ManagedRocksIterator iterator,
return displayTable(iterator, dbColumnFamilyDef, out(), schemaV3);
}

// If there are no parent directories, create them
File dirFile = new File(fileName);
xichen01 marked this conversation as resolved.
Show resolved Hide resolved
if (!dirFile.exists()) {
boolean flg = dirFile.mkdirs();
xichen01 marked this conversation as resolved.
Show resolved Hide resolved
if (!flg) {
throw new IOException("An exception occurred while creating " +
"the directory. Directorys: " + dirFile.getAbsolutePath());
}
}

// Write to file output
try (PrintWriter out = new PrintWriter(new BufferedWriter(
new PrintWriter(fileName, UTF_8.name())))) {
return displayTable(iterator, dbColumnFamilyDef, out, schemaV3);
while (iterator.get().isValid() && withinLimit(globalCount)) {
String fileNameTarget = recordsPerFile > 0 ? fileName + File.separator + fileSuffix++ :
fileName;
try (PrintWriter out = new PrintWriter(new BufferedWriter(
new PrintWriter(fileNameTarget, UTF_8.name())))) {
if (!displayTable(iterator, dbColumnFamilyDef, out, schemaV3)) {
return false;
}
}
}
return true;
}

private boolean displayTable(ManagedRocksIterator iterator,
Expand Down Expand Up @@ -314,7 +341,7 @@ private void processRecords(ManagedRocksIterator iterator,
}
}

while (withinLimit(count) && iterator.get().isValid() && !exception && !reachedEnd) {
while (withinLimit(globalCount) && iterator.get().isValid() && !exception && !reachedEnd) {
// if invalid endKey is given, it is ignored
if (null != endKey && Arrays.equals(iterator.get().key(), getValueObject(dbColumnFamilyDef, endKey))) {
reachedEnd = true;
Expand All @@ -326,6 +353,7 @@ private void processRecords(ManagedRocksIterator iterator,
// the record passes the filter
batch.add(new ByteArrayKeyValue(
iterator.get().key(), iterator.get().value()));
globalCount++;
count++;
if (batch.size() >= batchSize) {
while (logWriter.getInflightLogCount() > threadCount * 10L
Expand All @@ -343,6 +371,9 @@ private void processRecords(ManagedRocksIterator iterator,
}
}
iterator.get().next();
if ((recordsPerFile > 0) && (count >= recordsPerFile)) {
break;
}
}
if (!batch.isEmpty()) {
Future<Void> future = threadPool.submit(new Task(dbColumnFamilyDef,
Expand Down
Loading