-
Notifications
You must be signed in to change notification settings - Fork 63
Streaming telemetry implementation #971
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
kaxuna
wants to merge
10
commits into
main
Choose a base branch
from
streaming-telemetry-implementation
base: main
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.
Open
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
edb6952
feat: Add individual task telemetry to dumper system
kaxuna eeba8e7
added taskType
kaxuna 0839b32
fix: Add trailing comma to EventType enum for better compatibility
kaxuna 0d04095
Add streaming telemetry with FileSystem injection
kaxuna 92b7bb5
make telemetry streaming again
kaxuna 1612f0c
cache path and objectMapper is static now
kaxuna 1651280
addressed multithreading issues
kaxuna 9e5723a
Merge branch 'main' into streaming-telemetry-implementation
kaxuna d3e3b72
addressed multithreading issues
kaxuna 8fa7926
addressed multithreading issues
kaxuna 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
187 changes: 187 additions & 0 deletions
187
...in/java/com/google/edwmigration/dumper/application/dumper/DiskTelemetryWriteStrategy.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,187 @@ | ||
| /* | ||
| * Copyright 2022-2025 Google LLC | ||
| * Copyright 2013-2021 CompilerWorks | ||
| * | ||
| * Licensed 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 com.google.edwmigration.dumper.application.dumper; | ||
|
|
||
| import static java.nio.file.Files.newBufferedWriter; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.SerializationFeature; | ||
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
| import com.google.edwmigration.dumper.application.dumper.metrics.*; | ||
| import java.io.BufferedWriter; | ||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.*; | ||
| import java.util.ArrayDeque; | ||
| import java.util.Queue; | ||
| import net.harawata.appdirs.AppDirs; | ||
| import net.harawata.appdirs.AppDirsFactory; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Strategy implementation that writes telemetry data. This replaces the behavior when shouldWrite = | ||
| * true. | ||
| */ | ||
| public class DiskTelemetryWriteStrategy implements TelemetryWriteStrategy { | ||
kaxuna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private static final Logger logger = LoggerFactory.getLogger(DiskTelemetryWriteStrategy.class); | ||
| private static final String ALL_DUMPER_RUN_METRICS = "all-dumper-telemetry.jsonl"; | ||
| private static final String DUMPER_RUN_METRICS = "dumper-telemetry.jsonl"; | ||
| private final Path telemetryOsCachePath; | ||
| private final ObjectMapper MAPPER; | ||
kaxuna marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private final Queue<ClientTelemetry> bufferOs; | ||
| private final Queue<ClientTelemetry> bufferZip; | ||
| private FileSystem fileSystem; | ||
| private boolean telemetryOsCacheIsAvailable = true; | ||
|
|
||
| public DiskTelemetryWriteStrategy() { | ||
| bufferOs = new ArrayDeque<>(); | ||
| bufferZip = new ArrayDeque<>(); | ||
| MAPPER = createObjectMapper(); | ||
| telemetryOsCachePath = Paths.get(createTelemetryOsDirIfNotExists(), ALL_DUMPER_RUN_METRICS); | ||
kaxuna marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private static ObjectMapper createObjectMapper() { | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
|
|
||
| mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); | ||
| mapper.registerModule(new JavaTimeModule()); | ||
| mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||
|
|
||
| return mapper; | ||
| } | ||
|
|
||
| public void setZipFilePath(FileSystem fileSystem) { | ||
| this.fileSystem = fileSystem; | ||
| if (copyOsCacheToZip() && telemetryOsCacheIsAvailable) { | ||
| // these events were already registered in OsCache | ||
| bufferZip.clear(); | ||
| } | ||
| flush(); | ||
| } | ||
|
|
||
| @Override | ||
| public void process(ClientTelemetry clientTelemetry) { | ||
| logger.debug( | ||
| "Processing telemetry data with {} payload items", clientTelemetry.getPayload().size()); | ||
|
|
||
| if (telemetryOsCacheIsAvailable) { | ||
| bufferOs.add(clientTelemetry); | ||
| } | ||
| bufferZip.add(clientTelemetry); | ||
|
|
||
| flush(); | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() { | ||
| // this implementation uses buffer until zip file is created afterwords it is flushed per | ||
| // process | ||
| flushOsCache(); | ||
| flushZip(); | ||
| } | ||
|
|
||
| private void flushOsCache() { | ||
kaxuna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| while (telemetryOsCacheIsAvailable && !bufferOs.isEmpty()) { | ||
| try { | ||
| writeOnDisk(telemetryOsCachePath, MAPPER.writeValueAsString(bufferOs.poll())); | ||
| } catch (JsonProcessingException e) { | ||
| logger.warn("Failed to serialize telemetry to write in Os Cache", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void flushZip() { | ||
| while (fileSystem != null && !bufferZip.isEmpty()) { | ||
| try { | ||
| writeOnDisk( | ||
| fileSystem.getPath(DUMPER_RUN_METRICS), MAPPER.writeValueAsString(bufferZip.poll())); | ||
| } catch (JsonProcessingException e) { | ||
| logger.warn("Failed to serialize telemetry to write in Zip", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private String createTelemetryOsDirIfNotExists() { | ||
| AppDirs appDirs = AppDirsFactory.getInstance(); | ||
|
|
||
| String appName = "DWH-Dumper"; | ||
| String appVersion = ""; // All versions are accumulated in the same file | ||
| String appAuthor = "google"; // Optional, can be null | ||
| String cacheDir = appDirs.getUserCacheDir(appName, appVersion, appAuthor); | ||
| Path applicationCacheDirPath = Paths.get(cacheDir); | ||
| if (java.nio.file.Files.notExists(applicationCacheDirPath)) { | ||
| try { | ||
| java.nio.file.Files.createDirectories(applicationCacheDirPath); | ||
| logger.info("Created application telemetry cache directory: {}", applicationCacheDirPath); | ||
| } catch (IOException e) { | ||
| disableOsCache(); | ||
| logger.warn( | ||
| "Unable to create application telemetry cache directory : {}", applicationCacheDirPath); | ||
| } | ||
| } | ||
|
|
||
| return cacheDir; | ||
| } | ||
|
|
||
| private boolean copyOsCacheToZip() { | ||
| Path snapshotInZipPath = fileSystem.getPath(DUMPER_RUN_METRICS); | ||
| try { | ||
| Path parentInZip = snapshotInZipPath.getParent(); | ||
| if (parentInZip != null && java.nio.file.Files.notExists(parentInZip)) { | ||
| java.nio.file.Files.createDirectories(parentInZip); | ||
| } | ||
| java.nio.file.Files.copy( | ||
| telemetryOsCachePath, snapshotInZipPath, StandardCopyOption.REPLACE_EXISTING); | ||
| logger.debug( | ||
| "Copied Cached {} telemetry to zip file {}.", telemetryOsCachePath, snapshotInZipPath); | ||
| return true; | ||
| } catch (IOException e) { | ||
| logger.warn( | ||
| "Failed to copy cached telemetry from {} to ZIP at {}", | ||
| telemetryOsCachePath, | ||
| snapshotInZipPath, | ||
| e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** Appends the given summary lines for the current run to the external log file. */ | ||
| private static void writeOnDisk(Path path, String summaryLines) { | ||
| try (BufferedWriter writer = | ||
| newBufferedWriter( | ||
| path, | ||
| StandardCharsets.UTF_8, | ||
| StandardOpenOption.CREATE, | ||
| StandardOpenOption.APPEND); | ||
| PrintWriter printer = new PrintWriter(writer)) { | ||
|
|
||
| printer.println(summaryLines); | ||
| printer.flush(); | ||
| } catch (IOException e) { | ||
| logger.warn("Failed to append to external cumulative summary log: {}", path, e); | ||
| } | ||
| } | ||
|
|
||
| private void disableOsCache() { | ||
| telemetryOsCacheIsAvailable = false; | ||
| bufferOs.clear(); | ||
| } | ||
| } | ||
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
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.