|
1 | 1 | package com.linkedin.datahub.upgrade.impl;
|
2 | 2 |
|
3 | 3 | import com.codahale.metrics.MetricRegistry;
|
4 |
| -import com.codahale.metrics.Timer; |
5 | 4 | import com.linkedin.datahub.upgrade.Upgrade;
|
6 | 5 | import com.linkedin.datahub.upgrade.UpgradeCleanupStep;
|
7 | 6 | import com.linkedin.datahub.upgrade.UpgradeContext;
|
@@ -119,44 +118,60 @@ private UpgradeResult executeInternal(UpgradeContext context) {
|
119 | 118 | }
|
120 | 119 |
|
121 | 120 | private UpgradeStepResult executeStepInternal(UpgradeContext context, UpgradeStep step) {
|
122 |
| - int retryCount = step.retryCount(); |
123 |
| - UpgradeStepResult result = null; |
124 |
| - int maxAttempts = retryCount + 1; |
125 |
| - for (int i = 0; i < maxAttempts; i++) { |
126 |
| - try (Timer.Context completionTimer = |
127 |
| - MetricUtils.timer(MetricRegistry.name(step.id(), "completionTime")).time()) { |
128 |
| - try (Timer.Context executionTimer = |
129 |
| - MetricUtils.timer(MetricRegistry.name(step.id(), "executionTime")).time()) { |
130 |
| - result = step.executable().apply(context); |
131 |
| - } |
132 |
| - |
133 |
| - if (result == null) { |
134 |
| - // Failed to even retrieve a result. Create a default failure result. |
135 |
| - result = new DefaultUpgradeStepResult(step.id(), DataHubUpgradeState.FAILED); |
136 |
| - context |
137 |
| - .report() |
138 |
| - .addLine(String.format("Retrying %s more times...", maxAttempts - (i + 1))); |
139 |
| - MetricUtils.counter(MetricRegistry.name(step.id(), "retry")).inc(); |
140 |
| - } |
141 |
| - |
142 |
| - if (DataHubUpgradeState.SUCCEEDED.equals(result.result())) { |
143 |
| - MetricUtils.counter(MetricRegistry.name(step.id(), "succeeded")).inc(); |
144 |
| - break; |
145 |
| - } |
146 |
| - } catch (Exception e) { |
147 |
| - log.error("Caught exception during attempt {} of Step with id {}", i, step.id(), e); |
148 |
| - context |
149 |
| - .report() |
150 |
| - .addLine( |
151 |
| - String.format( |
152 |
| - "Caught exception during attempt %s of Step with id %s: %s", i, step.id(), e)); |
153 |
| - MetricUtils.counter(MetricRegistry.name(step.id(), "failed")).inc(); |
154 |
| - result = new DefaultUpgradeStepResult(step.id(), DataHubUpgradeState.FAILED); |
155 |
| - context.report().addLine(String.format("Retrying %s more times...", maxAttempts - (i + 1))); |
156 |
| - } |
157 |
| - } |
158 |
| - |
159 |
| - return result; |
| 121 | + return context |
| 122 | + .opContext() |
| 123 | + .withSpan( |
| 124 | + "completionTime", |
| 125 | + () -> { |
| 126 | + int retryCount = step.retryCount(); |
| 127 | + UpgradeStepResult result = null; |
| 128 | + int maxAttempts = retryCount + 1; |
| 129 | + for (int i = 0; i < maxAttempts; i++) { |
| 130 | + try { |
| 131 | + result = |
| 132 | + context |
| 133 | + .opContext() |
| 134 | + .withSpan( |
| 135 | + "executionTime", |
| 136 | + () -> step.executable().apply(context), |
| 137 | + "step.id", |
| 138 | + step.id(), |
| 139 | + MetricUtils.DROPWIZARD_NAME, |
| 140 | + MetricUtils.name(step.id(), "executionTime")); |
| 141 | + |
| 142 | + if (result == null) { |
| 143 | + // Failed to even retrieve a result. Create a default failure result. |
| 144 | + result = new DefaultUpgradeStepResult(step.id(), DataHubUpgradeState.FAILED); |
| 145 | + context |
| 146 | + .report() |
| 147 | + .addLine(String.format("Retrying %s more times...", maxAttempts - (i + 1))); |
| 148 | + MetricUtils.counter(MetricRegistry.name(step.id(), "retry")).inc(); |
| 149 | + } |
| 150 | + |
| 151 | + if (DataHubUpgradeState.SUCCEEDED.equals(result.result())) { |
| 152 | + MetricUtils.counter(MetricRegistry.name(step.id(), "succeeded")).inc(); |
| 153 | + break; |
| 154 | + } |
| 155 | + } catch (Exception e) { |
| 156 | + log.error( |
| 157 | + "Caught exception during attempt {} of Step with id {}", i, step.id(), e); |
| 158 | + context |
| 159 | + .report() |
| 160 | + .addLine( |
| 161 | + String.format( |
| 162 | + "Caught exception during attempt %s of Step with id %s: %s", |
| 163 | + i, step.id(), e)); |
| 164 | + MetricUtils.counter(MetricRegistry.name(step.id(), "failed")).inc(); |
| 165 | + result = new DefaultUpgradeStepResult(step.id(), DataHubUpgradeState.FAILED); |
| 166 | + context |
| 167 | + .report() |
| 168 | + .addLine(String.format("Retrying %s more times...", maxAttempts - (i + 1))); |
| 169 | + } |
| 170 | + } |
| 171 | + return result; |
| 172 | + }, |
| 173 | + MetricUtils.DROPWIZARD_METRIC, |
| 174 | + "true"); |
160 | 175 | }
|
161 | 176 |
|
162 | 177 | private void executeCleanupInternal(UpgradeContext context, UpgradeResult result) {
|
|
0 commit comments