Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.car.telemetry; |
| 18 | |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 19 | import static android.car.telemetry.CarTelemetryManager.ERROR_METRICS_CONFIG_ALREADY_EXISTS; |
| 20 | import static android.car.telemetry.CarTelemetryManager.ERROR_METRICS_CONFIG_NONE; |
| 21 | import static android.car.telemetry.CarTelemetryManager.ERROR_METRICS_CONFIG_UNKNOWN; |
| 22 | import static android.car.telemetry.CarTelemetryManager.ERROR_METRICS_CONFIG_VERSION_TOO_OLD; |
| 23 | |
Rui Qiu | 4624bac | 2021-10-12 16:41:18 -0700 | [diff] [blame] | 24 | import android.car.telemetry.MetricsConfigKey; |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 25 | import android.util.ArrayMap; |
Rui Qiu | 4e8a48e | 2021-11-09 17:14:26 -0800 | [diff] [blame^] | 26 | import android.util.AtomicFile; |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 27 | |
| 28 | import com.android.car.CarLog; |
| 29 | import com.android.internal.annotations.VisibleForTesting; |
Zhomart Mukhamejanov | 4d99b1e | 2021-10-15 21:36:52 -0700 | [diff] [blame] | 30 | import com.android.server.utils.Slogf; |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 31 | |
| 32 | import java.io.File; |
Rui Qiu | 4e8a48e | 2021-11-09 17:14:26 -0800 | [diff] [blame^] | 33 | import java.io.FileOutputStream; |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 34 | import java.io.IOException; |
| 35 | import java.nio.file.Files; |
Rui Qiu | 4624bac | 2021-10-12 16:41:18 -0700 | [diff] [blame] | 36 | import java.nio.file.Paths; |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 39 | import java.util.Map; |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * This class is responsible for storing, retrieving, and deleting {@link |
| 43 | * TelemetryProto.MetricsConfig}. All of the methods are blocking so the class should only be |
Rui Qiu | ac8b5ba | 2021-08-25 16:04:17 -0700 | [diff] [blame] | 44 | * accessed on the telemetry thread. |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 45 | */ |
Rui Qiu | 3d3806b | 2021-09-22 11:42:45 -0700 | [diff] [blame] | 46 | public class MetricsConfigStore { |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 47 | @VisibleForTesting |
| 48 | static final String METRICS_CONFIG_DIR = "metrics_configs"; |
| 49 | |
| 50 | private final File mConfigDirectory; |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 51 | private Map<String, TelemetryProto.MetricsConfig> mActiveConfigs; |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 52 | |
Rui Qiu | 3d3806b | 2021-09-22 11:42:45 -0700 | [diff] [blame] | 53 | public MetricsConfigStore(File rootDirectory) { |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 54 | mConfigDirectory = new File(rootDirectory, METRICS_CONFIG_DIR); |
| 55 | mConfigDirectory.mkdirs(); |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 56 | mActiveConfigs = new ArrayMap<>(); |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 57 | // TODO(b/197336485): Add expiration date check for MetricsConfig |
| 58 | for (File file : mConfigDirectory.listFiles()) { |
Rui Qiu | 4e8a48e | 2021-11-09 17:14:26 -0800 | [diff] [blame^] | 59 | AtomicFile atomicFile = new AtomicFile(file); |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 60 | try { |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 61 | TelemetryProto.MetricsConfig config = |
Rui Qiu | 4e8a48e | 2021-11-09 17:14:26 -0800 | [diff] [blame^] | 62 | TelemetryProto.MetricsConfig.parseFrom(atomicFile.readFully()); |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 63 | mActiveConfigs.put(config.getName(), config); |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 64 | } catch (IOException e) { |
| 65 | // TODO(b/197336655): record failure |
Rui Qiu | 4e8a48e | 2021-11-09 17:14:26 -0800 | [diff] [blame^] | 66 | atomicFile.delete(); |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns all active {@link TelemetryProto.MetricsConfig} from disk. |
| 73 | */ |
Rui Qiu | 3d3806b | 2021-09-22 11:42:45 -0700 | [diff] [blame] | 74 | public List<TelemetryProto.MetricsConfig> getActiveMetricsConfigs() { |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 75 | return new ArrayList<>(mActiveConfigs.values()); |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
Rui Qiu | 4624bac | 2021-10-12 16:41:18 -0700 | [diff] [blame] | 79 | * Stores the MetricsConfig to disk if it is valid. It checks both config name and version for |
| 80 | * validity. |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 81 | * |
| 82 | * @param metricsConfig the config to be persisted to disk. |
Rui Qiu | 4624bac | 2021-10-12 16:41:18 -0700 | [diff] [blame] | 83 | * @return {@link android.car.telemetry.CarTelemetryManager.MetricsConfigError} status code. |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 84 | */ |
Rui Qiu | 3d3806b | 2021-09-22 11:42:45 -0700 | [diff] [blame] | 85 | public int addMetricsConfig(TelemetryProto.MetricsConfig metricsConfig) { |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 86 | // TODO(b/197336485): Check expiration date for MetricsConfig |
Rui Qiu | 4624bac | 2021-10-12 16:41:18 -0700 | [diff] [blame] | 87 | if (metricsConfig.getVersion() <= 0) { |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 88 | return ERROR_METRICS_CONFIG_VERSION_TOO_OLD; |
Rui Qiu | 4624bac | 2021-10-12 16:41:18 -0700 | [diff] [blame] | 89 | } |
| 90 | if (mActiveConfigs.containsKey(metricsConfig.getName())) { |
| 91 | int currentVersion = mActiveConfigs.get(metricsConfig.getName()).getVersion(); |
| 92 | if (currentVersion > metricsConfig.getVersion()) { |
| 93 | return ERROR_METRICS_CONFIG_VERSION_TOO_OLD; |
| 94 | } else if (currentVersion == metricsConfig.getVersion()) { |
| 95 | return ERROR_METRICS_CONFIG_ALREADY_EXISTS; |
| 96 | } |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 97 | } |
Rui Qiu | f721b13 | 2021-09-10 12:41:09 -0700 | [diff] [blame] | 98 | mActiveConfigs.put(metricsConfig.getName(), metricsConfig); |
Rui Qiu | 4e8a48e | 2021-11-09 17:14:26 -0800 | [diff] [blame^] | 99 | AtomicFile atomicFile = new AtomicFile(new File(mConfigDirectory, metricsConfig.getName())); |
| 100 | FileOutputStream fos = null; |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 101 | try { |
Rui Qiu | 4e8a48e | 2021-11-09 17:14:26 -0800 | [diff] [blame^] | 102 | fos = atomicFile.startWrite(); |
| 103 | fos.write(metricsConfig.toByteArray()); |
| 104 | atomicFile.finishWrite(fos); |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 105 | } catch (IOException e) { |
| 106 | // TODO(b/197336655): record failure |
Rui Qiu | 4e8a48e | 2021-11-09 17:14:26 -0800 | [diff] [blame^] | 107 | atomicFile.failWrite(fos); |
Zhomart Mukhamejanov | 4d99b1e | 2021-10-15 21:36:52 -0700 | [diff] [blame] | 108 | Slogf.w(CarLog.TAG_TELEMETRY, "Failed to write metrics config to disk", e); |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 109 | return ERROR_METRICS_CONFIG_UNKNOWN; |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 110 | } |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 111 | return ERROR_METRICS_CONFIG_NONE; |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Rui Qiu | 4624bac | 2021-10-12 16:41:18 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Deletes the MetricsConfig from disk. |
| 116 | * |
| 117 | * @param key the unique identifier of the metrics config that should be deleted. |
| 118 | * @return true for successful removal, false otherwise. |
| 119 | */ |
| 120 | public boolean removeMetricsConfig(MetricsConfigKey key) { |
| 121 | String metricsConfigName = key.getName(); |
| 122 | if (!mActiveConfigs.containsKey(key.getName()) |
| 123 | || mActiveConfigs.get(key.getName()).getVersion() != key.getVersion()) { |
| 124 | return false; // no match found, nothing to remove |
Rui Qiu | 9f80c7c | 2021-09-28 15:15:36 -0700 | [diff] [blame] | 125 | } |
Rui Qiu | 4624bac | 2021-10-12 16:41:18 -0700 | [diff] [blame] | 126 | mActiveConfigs.remove(metricsConfigName); |
| 127 | try { |
| 128 | return Files.deleteIfExists(Paths.get( |
| 129 | mConfigDirectory.getAbsolutePath(), metricsConfigName)); |
| 130 | } catch (IOException e) { |
| 131 | Slogf.w(CarLog.TAG_TELEMETRY, "Failed to remove MetricsConfig: " + key.getName(), e); |
| 132 | // TODO(b/197336655): record failure |
| 133 | } |
| 134 | return false; |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 135 | } |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 136 | |
Rui Qiu | 3d3806b | 2021-09-22 11:42:45 -0700 | [diff] [blame] | 137 | /** Deletes all MetricsConfigs from disk. */ |
Rui Qiu | 9f80c7c | 2021-09-28 15:15:36 -0700 | [diff] [blame] | 138 | public void removeAllMetricsConfigs() { |
Rui Qiu | f721b13 | 2021-09-10 12:41:09 -0700 | [diff] [blame] | 139 | mActiveConfigs.clear(); |
| 140 | for (File file : mConfigDirectory.listFiles()) { |
Rui Qiu | 9f80c7c | 2021-09-28 15:15:36 -0700 | [diff] [blame] | 141 | if (!file.delete()) { |
Zhomart Mukhamejanov | 4d99b1e | 2021-10-15 21:36:52 -0700 | [diff] [blame] | 142 | Slogf.w(CarLog.TAG_TELEMETRY, "Failed to remove MetricsConfig: " + file.getName()); |
Rui Qiu | 9f80c7c | 2021-09-28 15:15:36 -0700 | [diff] [blame] | 143 | } |
Rui Qiu | f721b13 | 2021-09-10 12:41:09 -0700 | [diff] [blame] | 144 | } |
Rui Qiu | 9b9341c | 2021-08-25 10:39:04 -0700 | [diff] [blame] | 145 | } |
Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame] | 146 | } |