-
Notifications
You must be signed in to change notification settings - Fork 587
HDDS-12580. Set up Temporary DB for Storing Container Log Information #8072
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d7d8080
HDDS-12580. Set up Temporary RocksDB for Storing Container Log Inform…
9e4066a
HDDS-12580. Added the license and package info
cf0d7a6
HDDS-12580. Added methods to insert data into tables
f2e8dcf
HDDS-12580. Switch from RocksDB to SQLite
2578a5e
Updated Primary key
sreejasahithi a89797a
Moved SQL queries to properties file and centralized constants in DBC…
0590218
Merge branch 'HDDS-12580' of github.com:sreejasahithi/ozone into HDDS…
eaf82d8
Update container-log-db-queries.properties
sreejasahithi 58756a7
Updated ContainerDatanodeDatabase.java
sreejasahithi 989cae7
Modified visibility and added cleanup step
e623a71
Updated Primary key
sreejasahithi d1c2955
Updated queries and used JSONObject
9e7b257
Resolved merge conflict
597010f
Instead of JSONObject using a Pojo class
3381c22
Added descriptive log message
feef378
resolved checkstyle issue
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
228 changes: 228 additions & 0 deletions
228
.../src/main/java/org/apache/hadoop/ozone/containerlog/parser/ContainerDatanodeDatabase.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,228 @@ | ||
| /* | ||
| * 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.FileNotFoundException; | ||
| import java.io.InputStream; | ||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
| import java.util.stream.Collectors; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.sqlite.SQLiteConfig; | ||
|
|
||
|
|
||
| /** | ||
| * Datanode container Database. | ||
| */ | ||
|
|
||
| public class ContainerDatanodeDatabase { | ||
|
|
||
| private static Map<String, String> queries; | ||
| public static final String CONTAINER_KEY_DELIMITER = "#"; | ||
|
|
||
| static { | ||
| loadProperties(); | ||
| } | ||
|
|
||
| private static final Logger LOG = | ||
| LoggerFactory.getLogger(ContainerDatanodeDatabase.class); | ||
|
|
||
| private static void loadProperties() { | ||
| Properties props = new Properties(); | ||
| try (InputStream inputStream = ContainerDatanodeDatabase.class.getClassLoader() | ||
| .getResourceAsStream(DBConsts.PROPS_FILE)) { | ||
|
|
||
| if (inputStream != null) { | ||
| props.load(inputStream); | ||
| queries = props.entrySet().stream() | ||
| .collect(Collectors.toMap( | ||
| e -> e.getKey().toString(), | ||
| e -> e.getValue().toString() | ||
| )); | ||
| } else { | ||
| throw new FileNotFoundException("Property file '" + DBConsts.PROPS_FILE + "' not found."); | ||
| } | ||
| } catch (Exception e) { | ||
| LOG.error(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private static Connection getConnection() throws Exception { | ||
| Class.forName(DBConsts.DRIVER); | ||
|
|
||
| SQLiteConfig config = new SQLiteConfig(); | ||
|
|
||
| config.setJournalMode(SQLiteConfig.JournalMode.OFF); | ||
| config.setCacheSize(DBConsts.CACHE_SIZE); | ||
| config.setLockingMode(SQLiteConfig.LockingMode.EXCLUSIVE); | ||
| config.setSynchronous(SQLiteConfig.SynchronousMode.OFF); | ||
| config.setTempStore(SQLiteConfig.TempStore.MEMORY); | ||
|
|
||
| return DriverManager.getConnection(DBConsts.CONNECTION_PREFIX + DBConsts.DATABASE_NAME, config.toProperties()); | ||
| } | ||
|
|
||
| public void createDatanodeContainerLogTable() throws SQLException { | ||
| String createTableSQL = queries.get("CREATE_DATANODE_CONTAINER_LOG_TABLE"); | ||
| try (Connection connection = getConnection(); | ||
| Statement dropStmt = connection.createStatement(); | ||
| Statement createStmt = connection.createStatement()) { | ||
| dropTable(DBConsts.DATANODE_CONTAINER_LOG_TABLE_NAME, dropStmt); | ||
| createStmt.execute(createTableSQL); | ||
| createDatanodeContainerIndex(createStmt); | ||
| } catch (SQLException e) { | ||
| LOG.error("Error while creating the table: {}", e.getMessage()); | ||
| throw e; | ||
| } catch (Exception e) { | ||
| LOG.error(e.getMessage()); | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private void createContainerLogTable() throws SQLException { | ||
| String createTableSQL = queries.get("CREATE_CONTAINER_LOG_TABLE"); | ||
| try (Connection connection = getConnection(); | ||
| Statement dropStmt = connection.createStatement(); | ||
| Statement createStmt = connection.createStatement()) { | ||
| dropTable(DBConsts.CONTAINER_LOG_TABLE_NAME, dropStmt); | ||
| createStmt.execute(createTableSQL); | ||
| } catch (SQLException e) { | ||
| LOG.error("Error while creating the table: {}", e.getMessage()); | ||
| throw e; | ||
| } catch (Exception e) { | ||
| LOG.error(e.getMessage()); | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| public void insertContainerDatanodeData(String key, List<DatanodeContainerInfo> transitionList) throws SQLException { | ||
| String[] parts = key.split(CONTAINER_KEY_DELIMITER); | ||
| if (parts.length != 2) { | ||
| System.err.println("Invalid key format: " + key); | ||
| return; | ||
| } | ||
|
|
||
| long containerId = Long.parseLong(parts[0]); | ||
| long datanodeId = Long.parseLong(parts[1]); | ||
|
|
||
| String insertSQL = queries.get("INSERT_DATANODE_CONTAINER_LOG"); | ||
|
|
||
| try (Connection connection = getConnection(); | ||
| PreparedStatement preparedStatement = connection.prepareStatement(insertSQL)) { | ||
sreejasahithi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| int count = 0; | ||
|
|
||
| for (DatanodeContainerInfo info : transitionList) { | ||
| preparedStatement.setLong(1, datanodeId); | ||
| preparedStatement.setLong(2, containerId); | ||
| preparedStatement.setString(3, info.getTimestamp()); | ||
| preparedStatement.setString(4, info.getState()); | ||
| preparedStatement.setLong(5, info.getBcsid()); | ||
| preparedStatement.setString(6, info.getErrorMessage()); | ||
| preparedStatement.setString(7, info.getLogLevel()); | ||
| preparedStatement.setInt(8, info.getIndexValue()); | ||
| preparedStatement.addBatch(); | ||
|
|
||
| count++; | ||
|
|
||
| if (count % DBConsts.BATCH_SIZE == 0) { | ||
| preparedStatement.executeBatch(); | ||
sreejasahithi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| count = 0; | ||
| } | ||
| } | ||
|
|
||
| if (count != 0) { | ||
| preparedStatement.executeBatch(); | ||
| } | ||
| } catch (SQLException e) { | ||
| LOG.error("Failed to insert container log for container {} on datanode {}", containerId, datanodeId, e); | ||
| throw e; | ||
| } catch (Exception e) { | ||
| LOG.error(e.getMessage()); | ||
| throw new RuntimeException(e); | ||
sumitagrawl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private void createDatanodeContainerIndex(Statement stmt) throws SQLException { | ||
| String createIndexSQL = queries.get("CREATE_DATANODE_CONTAINER_INDEX"); | ||
| stmt.execute(createIndexSQL); | ||
| } | ||
|
|
||
| public void insertLatestContainerLogData() throws SQLException { | ||
| createContainerLogTable(); | ||
| String selectSQL = queries.get("SELECT_LATEST_CONTAINER_LOG"); | ||
| String insertSQL = queries.get("INSERT_CONTAINER_LOG"); | ||
|
|
||
| try (Connection connection = getConnection(); | ||
| PreparedStatement selectStmt = connection.prepareStatement(selectSQL); | ||
| ResultSet resultSet = selectStmt.executeQuery(); | ||
| PreparedStatement insertStmt = connection.prepareStatement(insertSQL)) { | ||
|
|
||
| int count = 0; | ||
|
|
||
| while (resultSet.next()) { | ||
| long datanodeId = resultSet.getLong("datanode_id"); | ||
| long containerId = resultSet.getLong("container_id"); | ||
| String containerState = resultSet.getString("container_state"); | ||
| long bcsid = resultSet.getLong("bcsid"); | ||
| try { | ||
| insertStmt.setLong(1, datanodeId); | ||
| insertStmt.setLong(2, containerId); | ||
| insertStmt.setString(3, containerState); | ||
| insertStmt.setLong(4, bcsid); | ||
| insertStmt.addBatch(); | ||
|
|
||
| count++; | ||
|
|
||
| if (count % DBConsts.BATCH_SIZE == 0) { | ||
| insertStmt.executeBatch(); | ||
| count = 0; | ||
| } | ||
| } catch (SQLException e) { | ||
| LOG.error("Failed to insert container log entry for container {} on datanode {} ", | ||
| containerId, datanodeId, e); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| if (count != 0) { | ||
| insertStmt.executeBatch(); | ||
| } | ||
| } catch (SQLException e) { | ||
| LOG.error("Failed to insert container log entry: {}", e.getMessage()); | ||
| throw e; | ||
| } catch (Exception e) { | ||
| LOG.error(e.getMessage()); | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private void dropTable(String tableName, Statement stmt) throws SQLException { | ||
| String dropTableSQL = queries.get("DROP_TABLE").replace("{table_name}", tableName); | ||
| stmt.executeUpdate(dropTableSQL); | ||
| } | ||
|
|
||
| } | ||
|
|
||
38 changes: 38 additions & 0 deletions
38
hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/containerlog/parser/DBConsts.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,38 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** | ||
| * Constants used for ContainerDatanodeDatabase. | ||
| */ | ||
| public final class DBConsts { | ||
|
|
||
| private DBConsts() { | ||
| //Never constructed | ||
| } | ||
|
|
||
| public static final String DRIVER = "org.sqlite.JDBC"; | ||
| public static final String CONNECTION_PREFIX = "jdbc:sqlite:"; | ||
| public static final String DATABASE_NAME = "container_datanode.db"; | ||
| public static final String PROPS_FILE = "container-log-db-queries.properties"; | ||
| public static final int CACHE_SIZE = 1000000; | ||
| public static final int BATCH_SIZE = 1000; | ||
| public static final String DATANODE_CONTAINER_LOG_TABLE_NAME = "DatanodeContainerLogTable"; | ||
| public static final String CONTAINER_LOG_TABLE_NAME = "ContainerLogTable"; | ||
|
|
||
| } |
93 changes: 93 additions & 0 deletions
93
...ools/src/main/java/org/apache/hadoop/ozone/containerlog/parser/DatanodeContainerInfo.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,93 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** | ||
| *Holds information about a container. | ||
| */ | ||
|
|
||
| public class DatanodeContainerInfo { | ||
|
|
||
| private String timestamp; | ||
| private String state; | ||
| private long bcsid; | ||
| private String errorMessage; | ||
| private String logLevel; | ||
| private int indexValue; | ||
|
|
||
| public DatanodeContainerInfo() { | ||
| } | ||
| public DatanodeContainerInfo(String timestamp, String state, long bcsid, String errorMessage, | ||
| String logLevel, int indexValue) { | ||
| this.timestamp = timestamp; | ||
| this.state = state; | ||
| this.bcsid = bcsid; | ||
| this.errorMessage = errorMessage; | ||
| this.logLevel = logLevel; | ||
| this.indexValue = indexValue; | ||
| } | ||
|
|
||
| public String getTimestamp() { | ||
| return timestamp; | ||
| } | ||
|
|
||
| public void setTimestamp(String timestamp) { | ||
| this.timestamp = timestamp; | ||
| } | ||
|
|
||
| public String getState() { | ||
| return state; | ||
| } | ||
|
|
||
| public void setState(String state) { | ||
| this.state = state; | ||
| } | ||
|
|
||
| public long getBcsid() { | ||
| return bcsid; | ||
| } | ||
|
|
||
| public void setBcsid(long bcsid) { | ||
| this.bcsid = bcsid; | ||
| } | ||
|
|
||
| public String getErrorMessage() { | ||
| return errorMessage; | ||
| } | ||
|
|
||
| public void setErrorMessage(String errorMessage) { | ||
| this.errorMessage = errorMessage; | ||
| } | ||
|
|
||
| public String getLogLevel() { | ||
| return logLevel; | ||
| } | ||
|
|
||
| public void setLogLevel(String logLevel) { | ||
| this.logLevel = logLevel; | ||
| } | ||
|
|
||
| public int getIndexValue() { | ||
| return indexValue; | ||
| } | ||
|
|
||
| public void setIndexValue(int indexValue) { | ||
| this.indexValue = indexValue; | ||
| } | ||
|
|
||
| } |
22 changes: 22 additions & 0 deletions
22
...p-ozone/tools/src/main/java/org/apache/hadoop/ozone/containerlog/parser/package-info.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,22 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * Classes used for Ozone Container Log parser tool. | ||
| */ | ||
|
|
||
| package org.apache.hadoop.ozone.containerlog.parser; |
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.