-
Notifications
You must be signed in to change notification settings - Fork 619
HDDS-12581. Multi-threaded Log File Parsing with Batch Updates to DB #8254
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
06f7559
HDDS-12581. Multi-threaded Log File Parsing with Batch Updates to DB
534c5d4
fixed notifyall
195de53
removed unnecessary comments and print statements
1b83e3b
Updated locking and using batch list
00cdec2
Removed locking and using Builder design pattern
ad568be
defined constants and simplified builder usage inside switch case
79dae79
Removed Unwanted chnages
65e38eb
Added javadoc
acaa5f0
Updated datanode ID parsing logic
51f56a6
fixed pmd issue
74f745f
Updated sql exception handling
296a776
Fixed checkstyle
dbf51ab
Fixed threadCount validation, path checking and removed premature exe…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
222 changes: 222 additions & 0 deletions
222
...ols/src/main/java/org/apache/hadoop/ozone/containerlog/parser/ContainerLogFileParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| /* | ||
| * 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.ozone.containerlog.parser; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.sql.SQLException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * Parses container log files and stores container details into a database. | ||
| * Uses multithreading to process multiple log files concurrently. | ||
| */ | ||
|
|
||
| public class ContainerLogFileParser { | ||
|
|
||
| private ExecutorService executorService; | ||
| private static final int MAX_OBJ_IN_LIST = 5000; | ||
|
|
||
| private static final String LOG_FILE_MARKER = ".log."; | ||
| private static final String LOG_LINE_SPLIT_REGEX = " \\| "; | ||
| private static final String KEY_VALUE_SPLIT_REGEX = "="; | ||
| private static final String KEY_ID = "ID"; | ||
| private static final String KEY_BCSID = "BCSID"; | ||
| private static final String KEY_STATE = "State"; | ||
| private static final String KEY_INDEX = "Index"; | ||
| private final AtomicBoolean hasErrorOccurred = new AtomicBoolean(false); | ||
|
|
||
| /** | ||
| * Scans the specified log directory, processes each file in a separate thread. | ||
| * Expects each log filename to follow the format: dn-container-<roll over number>.log.<datanodeId> | ||
| * | ||
| * @param logDirectoryPath Path to the directory containing container log files. | ||
| * @param dbstore Database object used to persist parsed container data. | ||
| * @param threadCount Number of threads to use for parallel processing. | ||
| */ | ||
|
|
||
| public void processLogEntries(String logDirectoryPath, ContainerDatanodeDatabase dbstore, int threadCount) | ||
| throws SQLException { | ||
| try (Stream<Path> paths = Files.walk(Paths.get(logDirectoryPath))) { | ||
|
|
||
| List<Path> files = paths.filter(Files::isRegularFile).collect(Collectors.toList()); | ||
|
|
||
| executorService = Executors.newFixedThreadPool(threadCount); | ||
|
|
||
| CountDownLatch latch = new CountDownLatch(files.size()); | ||
| for (Path file : files) { | ||
| Path fileNamePath = file.getFileName(); | ||
| String fileName = (fileNamePath != null) ? fileNamePath.toString() : ""; | ||
|
|
||
| int pos = fileName.indexOf(LOG_FILE_MARKER); | ||
| if (pos == -1) { | ||
| System.out.println("Filename format is incorrect (missing .log.): " + fileName); | ||
| continue; | ||
| } | ||
|
|
||
| String datanodeId = fileName.substring(pos + 5); | ||
|
|
||
| if (datanodeId.isEmpty()) { | ||
| System.out.println("Filename format is incorrect, datanodeId is missing or empty: " + fileName); | ||
| continue; | ||
| } | ||
|
|
||
| executorService.submit(() -> { | ||
|
|
||
| String threadName = Thread.currentThread().getName(); | ||
| try { | ||
| System.out.println(threadName + " is starting to process file: " + file.toString()); | ||
| processFile(file.toString(), dbstore, datanodeId); | ||
| } catch (Exception e) { | ||
| System.err.println("Thread " + threadName + " is stopping to process the file: " + file.toString() + | ||
| " due to SQLException: " + e.getMessage()); | ||
| hasErrorOccurred.set(true); | ||
| executorService.shutdown(); | ||
|
aryangupta1998 marked this conversation as resolved.
Outdated
|
||
| } finally { | ||
| try { | ||
| latch.countDown(); | ||
| System.out.println(threadName + " finished processing file: " + file.toString() + | ||
| ", Latch count after countdown: " + latch.getCount()); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| latch.await(); | ||
|
|
||
| executorService.shutdown(); | ||
|
|
||
| if (hasErrorOccurred.get()) { | ||
| throw new SQLException("Log file processing failed."); | ||
| } | ||
|
|
||
| } catch (IOException | InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } catch (NumberFormatException e) { | ||
| System.err.println("Invalid datanode ID"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Processes a single container log file and extracts container details. | ||
| * Parses, batches, and writes valid container log entries into the database. | ||
| * | ||
| * @param logFilePath Path to the log file. | ||
| * @param dbstore Database object used to persist parsed container data. | ||
| * @param datanodeId Datanode ID derived from the log filename. | ||
| */ | ||
|
|
||
| private void processFile(String logFilePath, ContainerDatanodeDatabase dbstore, String datanodeId) | ||
| throws SQLException { | ||
| List<DatanodeContainerInfo> batchList = new ArrayList<>(5100); | ||
|
aryangupta1998 marked this conversation as resolved.
Outdated
|
||
|
|
||
| try (BufferedReader reader = Files.newBufferedReader(Paths.get(logFilePath), StandardCharsets.UTF_8)) { | ||
| String line; | ||
| while ((line = reader.readLine()) != null) { | ||
| String[] parts = line.split(LOG_LINE_SPLIT_REGEX); | ||
| String timestamp = parts[0].trim(); | ||
| String logLevel = parts[1].trim(); | ||
| String id = null, index = null; | ||
|
|
||
| DatanodeContainerInfo.Builder builder = new DatanodeContainerInfo.Builder() | ||
| .setDatanodeId(datanodeId) | ||
| .setTimestamp(timestamp) | ||
| .setLogLevel(logLevel); | ||
|
|
||
| for (int i = 2; i < parts.length; i++) { | ||
| String part = parts[i].trim(); | ||
|
|
||
| if (part.contains(KEY_VALUE_SPLIT_REGEX)) { | ||
| String[] keyValue = part.split(KEY_VALUE_SPLIT_REGEX, 2); | ||
| if (keyValue.length == 2) { | ||
| String key = keyValue[0].trim(); | ||
| String value = keyValue[1].trim(); | ||
|
|
||
| switch (key) { | ||
| case KEY_ID: | ||
| id = value; | ||
| builder.setContainerId(Long.parseLong(value)); | ||
| break; | ||
| case KEY_BCSID: | ||
| builder.setBcsid(Long.parseLong(value)); | ||
| break; | ||
| case KEY_STATE: | ||
| builder.setState(value.replace("|", "").trim()); | ||
| break; | ||
| case KEY_INDEX: | ||
| index = value; | ||
| builder.setIndexValue(Integer.parseInt(value)); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| } else { | ||
| if (!part.isEmpty()) { | ||
| builder.setErrorMessage(part.replace("|", "").trim()); | ||
| } else { | ||
| builder.setErrorMessage("No error"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (index == null || !index.equals("0")) { | ||
| continue; //Currently only ratis replicated containers are considered. | ||
| } | ||
|
|
||
| if (id != null) { | ||
| try { | ||
| batchList.add(builder.build()); | ||
|
|
||
| if (batchList.size() >= MAX_OBJ_IN_LIST) { | ||
| dbstore.insertContainerDatanodeData(batchList); | ||
| batchList.clear(); | ||
| } | ||
| } catch (SQLException e) { | ||
| throw new SQLException(e.getMessage()); | ||
| } catch (Exception e) { | ||
| System.err.println( | ||
| "Error processing the batch for container: " + id + " at datanode: " + datanodeId); | ||
| e.printStackTrace(); | ||
| } | ||
| } else { | ||
| System.err.println("Log line does not have all required fields: " + line); | ||
| } | ||
| } | ||
| if (!batchList.isEmpty()) { | ||
| dbstore.insertContainerDatanodeData(batchList); | ||
| batchList.clear(); | ||
| } | ||
|
|
||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.