-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Implement size-based file rotation for FileBasedEventLogger #8415
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
Open
Ironankit525
wants to merge
6
commits into
apache:master
Choose a base branch
from
Ironankit525:feature/eventlogger-file-rotation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−24
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
37ef064
Implement size-based file rotation for FileBasedEventLogger
Ironankit525 1244aab
Address review comments: use BYTES_PER_MB constant and commit writeLo…
Ironankit525 e8df12f
Apply suggestion from @Copilot
rzo1 c6a148e
Update storm-client/test/jvm/org/apache/storm/metric/FileBasedEventLo…
rzo1 936a6de
x
rzo1 6fc4f3c
Address remaining Copilot PR review comments
Ironankit525 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
Some comments aren't visible on the classic Files Changed page.
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
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
144 changes: 144 additions & 0 deletions
144
storm-client/test/jvm/org/apache/storm/metric/FileBasedEventLoggerTest.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,144 @@ | ||
| /* | ||
| * 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.storm.metric; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.storm.Config; | ||
| import org.apache.storm.task.TopologyContext; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class FileBasedEventLoggerTest { | ||
|
|
||
| private Path tempDir; | ||
| private FileBasedEventLogger eventLogger; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() throws IOException { | ||
| tempDir = Files.createTempDirectory("storm-eventlogger-test"); | ||
| eventLogger = new FileBasedEventLogger(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() throws IOException { | ||
| eventLogger.close(); | ||
| if (tempDir != null) { | ||
| Files.walk(tempDir) | ||
| .map(Path::toFile) | ||
| .forEach(File::delete); | ||
| tempDir.toFile().delete(); | ||
| } | ||
rzo1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private TopologyContext mockTopologyContext() { | ||
| TopologyContext context = mock(TopologyContext.class); | ||
| when(context.getStormId()).thenReturn("test-topology-1"); | ||
| when(context.getThisWorkerPort()).thenReturn(6700); | ||
| return context; | ||
| } | ||
|
|
||
| @Test | ||
| public void testFileRotation() throws IOException, InterruptedException { | ||
| Map<String, Object> conf = new HashMap<>(); | ||
| conf.put(Config.STORM_WORKERS_ARTIFACTS_DIR, tempDir.toAbsolutePath().toString()); | ||
| // We set rotation to be 1MB to trigger it easily, but we'll need to write | ||
| // a lot. Alternatively, we can use a very small value, but we need an int >= 1. | ||
| // Wait, Config is by MB. If we set it to 1, we still need to write 1MB. | ||
| // Let's reflection inject a smaller value for tests? No, Storm uses config. | ||
| // We will just use `1` MB and write a large string a few times. | ||
| conf.put(Config.TOPOLOGY_EVENTLOGGER_ROTATION_SIZE_MB, 1); | ||
| conf.put(Config.TOPOLOGY_EVENTLOGGER_MAX_RETAINED_FILES, 2); | ||
|
|
||
| eventLogger.prepare(conf, new HashMap<>(), mockTopologyContext()); | ||
|
|
||
| // 1 MB = 1048576 bytes | ||
| // We create an event message that is about 100KB, write it 11 times to exceed 1MB. | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (int i = 0; i < 100_000; i++) { | ||
| sb.append("A"); // 1 byte | ||
| } | ||
| String largeValue = sb.toString(); | ||
|
|
||
| List<Object> values = new ArrayList<>(); | ||
| values.add(largeValue); | ||
|
|
||
| // This toString() will add some bytes overhead, so each event is ~ 100KB. | ||
| IEventLogger.EventInfo eventInfo = new IEventLogger.EventInfo( | ||
| System.currentTimeMillis(), "test-component", 1, "msgId", values); | ||
|
|
||
| // Write 10 times -> ~1 MB | ||
| for (int i = 0; i < 10; i++) { | ||
| eventLogger.log(eventInfo); | ||
| } | ||
|
|
||
| // Wait a bit for flush if any (though rotation is synchronous in write) | ||
| Thread.sleep(100); | ||
|
|
||
| Path expectedLogDir = tempDir.resolve("test-topology-1").resolve("6700"); | ||
| Path logFile = expectedLogDir.resolve("events.log"); | ||
| Path logFile1 = expectedLogDir.resolve("events.log.1"); | ||
| Path logFile2 = expectedLogDir.resolve("events.log.2"); | ||
|
|
||
| // The first 10 writes should be in one file, almost 1 MB. | ||
| assertTrue(Files.exists(logFile)); | ||
|
|
||
| // Write 2 more times to push it over 1MB | ||
| eventLogger.log(eventInfo); | ||
| eventLogger.log(eventInfo); | ||
|
|
||
| Thread.sleep(100); | ||
|
|
||
| // Now we expect events.log.1 to exist and events.log to be new | ||
| assertTrue(Files.exists(logFile1), "Rotated file events.log.1 should exist"); | ||
|
|
||
| // Write 12 more times to push over 1MB again | ||
| for (int i = 0; i < 12; i++) { | ||
| eventLogger.log(eventInfo); | ||
| } | ||
|
|
||
| Thread.sleep(100); | ||
|
|
||
| // Now events.log.2 and events.log.1 and events.log should exist | ||
| assertTrue(Files.exists(logFile2), "Rotated file events.log.2 should exist"); | ||
|
|
||
| // Write 12 MORE times to push over 1MB again | ||
| for (int i = 0; i < 12; i++) { | ||
| eventLogger.log(eventInfo); | ||
| } | ||
|
|
||
| Thread.sleep(100); | ||
|
|
||
| // max config was 2, so events.log.3 should NOT exist, and events.log.2 | ||
| // should exist. | ||
| Path logFile3 = expectedLogDir.resolve("events.log.3"); | ||
| assertTrue(!Files.exists(logFile3), "Rotated file events.log.3 should not exist"); | ||
| assertTrue(Files.exists(logFile2), "Rotated file events.log.2 should exist"); | ||
| } | ||
| } | ||
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.