Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | package com.android.tradefed.invoker; |
| 17 | |
jdesprez | 5c27266 | 2017-11-28 02:42:27 -0800 | [diff] [blame] | 18 | import com.android.tradefed.build.IBuildInfo; |
jdesprez | c9f8149 | 2017-05-30 15:59:05 -0700 | [diff] [blame] | 19 | import com.android.tradefed.log.LogUtil.CLog; |
Julien Desprez | 7fc5425 | 2020-04-30 15:08:45 -0700 | [diff] [blame^] | 20 | import com.android.tradefed.result.FailureDescription; |
jdesprez | 2ffb1bd | 2018-03-19 10:37:23 -0700 | [diff] [blame] | 21 | import com.android.tradefed.result.ILogSaverListener; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 22 | import com.android.tradefed.result.ITestInvocationListener; |
jdesprez | 2ffb1bd | 2018-03-19 10:37:23 -0700 | [diff] [blame] | 23 | import com.android.tradefed.result.InputStreamSource; |
| 24 | import com.android.tradefed.result.LogDataType; |
| 25 | import com.android.tradefed.result.LogFile; |
Julien Desprez | e6b756b | 2018-04-16 10:56:38 -0700 | [diff] [blame] | 26 | import com.android.tradefed.result.LogSaverResultForwarder; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 27 | import com.android.tradefed.result.ResultForwarder; |
jdesprez | c9f8149 | 2017-05-30 15:59:05 -0700 | [diff] [blame] | 28 | import com.android.tradefed.util.TimeUtil; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 29 | |
jdesprez | 5c27266 | 2017-11-28 02:42:27 -0800 | [diff] [blame] | 30 | import java.util.ArrayList; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 31 | import java.util.List; |
jdesprez | 5c27266 | 2017-11-28 02:42:27 -0800 | [diff] [blame] | 32 | import java.util.Map.Entry; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * A {@link ResultForwarder} that combines the results of a sharded test invocations. It only |
| 36 | * reports completion of the invocation to the listeners once all sharded invocations are complete. |
jdesprez | bf750e1 | 2017-04-03 12:18:47 -0700 | [diff] [blame] | 37 | * |
| 38 | * <p>This class is not thread safe. It is expected that clients will lock on this class when |
| 39 | * sending test results, to prevent invocation callbacks from being called out of order. |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 40 | */ |
jdesprez | 2ffb1bd | 2018-03-19 10:37:23 -0700 | [diff] [blame] | 41 | public class ShardMasterResultForwarder extends ResultForwarder implements ILogSaverListener { |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 42 | |
jdesprez | c9f8149 | 2017-05-30 15:59:05 -0700 | [diff] [blame] | 43 | private final int mInitCount; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 44 | private int mShardsRemaining; |
jdesprez | 671d0d2 | 2018-01-29 16:04:15 -0800 | [diff] [blame] | 45 | private long mTotalElapsed = 0L; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 46 | private boolean mStartReported = false; |
| 47 | |
jdesprez | 671d0d2 | 2018-01-29 16:04:15 -0800 | [diff] [blame] | 48 | private long mFirstShardEndTime = 0L; |
jdesprez | 57f2df4 | 2017-10-31 11:58:23 -0700 | [diff] [blame] | 49 | private IInvocationContext mOriginalContext; |
jdesprez | 5c27266 | 2017-11-28 02:42:27 -0800 | [diff] [blame] | 50 | private List<IInvocationContext> mShardContextList; |
jdesprez | 57f2df4 | 2017-10-31 11:58:23 -0700 | [diff] [blame] | 51 | private int shardIndex = 0; |
jdesprez | c9f8149 | 2017-05-30 15:59:05 -0700 | [diff] [blame] | 52 | |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Create a {@link ShardMasterResultForwarder}. |
| 55 | * |
| 56 | * @param listeners the list of {@link ITestInvocationListener} to forward results to when all |
jdesprez | 2ffb1bd | 2018-03-19 10:37:23 -0700 | [diff] [blame] | 57 | * shards are completed |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 58 | * @param expectedShards the number of shards |
| 59 | */ |
jdesprez | 2ffb1bd | 2018-03-19 10:37:23 -0700 | [diff] [blame] | 60 | public ShardMasterResultForwarder(List<ITestInvocationListener> listeners, int expectedShards) { |
| 61 | super(listeners); |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 62 | mShardsRemaining = expectedShards; |
jdesprez | c9f8149 | 2017-05-30 15:59:05 -0700 | [diff] [blame] | 63 | mInitCount = expectedShards; |
jdesprez | 5c27266 | 2017-11-28 02:42:27 -0800 | [diff] [blame] | 64 | mShardContextList = new ArrayList<>(); |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Julien Desprez | d0c379a | 2016-11-04 11:00:54 +0000 | [diff] [blame] | 67 | /** |
| 68 | * {@inheritDoc} |
Julien Desprez | d0c379a | 2016-11-04 11:00:54 +0000 | [diff] [blame] | 69 | */ |
Julien Desprez | 53353e6 | 2016-08-12 15:24:33 +0100 | [diff] [blame] | 70 | @Override |
| 71 | public void invocationStarted(IInvocationContext context) { |
| 72 | if (!mStartReported) { |
jdesprez | 57f2df4 | 2017-10-31 11:58:23 -0700 | [diff] [blame] | 73 | mOriginalContext = context; |
Julien Desprez | 53353e6 | 2016-08-12 15:24:33 +0100 | [diff] [blame] | 74 | super.invocationStarted(context); |
| 75 | mStartReported = true; |
jdesprez | 57f2df4 | 2017-10-31 11:58:23 -0700 | [diff] [blame] | 76 | } else { |
| 77 | // Track serials used in each shard. |
| 78 | mOriginalContext.addSerialsFromShard(shardIndex, context.getSerials()); |
jdesprez | 5c27266 | 2017-11-28 02:42:27 -0800 | [diff] [blame] | 79 | mShardContextList.add(context); |
jdesprez | 57f2df4 | 2017-10-31 11:58:23 -0700 | [diff] [blame] | 80 | shardIndex++; |
Julien Desprez | 53353e6 | 2016-08-12 15:24:33 +0100 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 84 | /** |
| 85 | * {@inheritDoc} |
| 86 | */ |
| 87 | @Override |
| 88 | public void invocationFailed(Throwable cause) { |
| 89 | // one of the shards failed. Fail the whole invocation |
| 90 | // TODO: does any extra logging need to be done ? |
| 91 | super.invocationFailed(cause); |
| 92 | } |
| 93 | |
Julien Desprez | 7fc5425 | 2020-04-30 15:08:45 -0700 | [diff] [blame^] | 94 | /** {@inheritDoc} */ |
| 95 | @Override |
| 96 | public void invocationFailed(FailureDescription failure) { |
| 97 | // one of the shards failed. Fail the whole invocation |
| 98 | super.invocationFailed(failure); |
| 99 | } |
| 100 | |
Julien Desprez | d0c379a | 2016-11-04 11:00:54 +0000 | [diff] [blame] | 101 | /** |
| 102 | * {@inheritDoc} |
| 103 | */ |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 104 | @Override |
| 105 | public void invocationEnded(long elapsedTime) { |
| 106 | mTotalElapsed += elapsedTime; |
jdesprez | c9f8149 | 2017-05-30 15:59:05 -0700 | [diff] [blame] | 107 | if (mInitCount == mShardsRemaining) { |
| 108 | mFirstShardEndTime = System.currentTimeMillis(); |
| 109 | } |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 110 | mShardsRemaining--; |
| 111 | if (mShardsRemaining <= 0) { |
jdesprez | c9f8149 | 2017-05-30 15:59:05 -0700 | [diff] [blame] | 112 | // TODO: consider logging all shard final times. |
| 113 | CLog.i( |
| 114 | "There was %s between the first and last shard ended.", |
| 115 | TimeUtil.formatElapsedTime(System.currentTimeMillis() - mFirstShardEndTime)); |
jdesprez | 5c27266 | 2017-11-28 02:42:27 -0800 | [diff] [blame] | 116 | copyShardBuildInfoToMain(mOriginalContext, mShardContextList); |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 117 | super.invocationEnded(mTotalElapsed); |
| 118 | } |
| 119 | } |
jdesprez | 5c27266 | 2017-11-28 02:42:27 -0800 | [diff] [blame] | 120 | |
jdesprez | 2ffb1bd | 2018-03-19 10:37:23 -0700 | [diff] [blame] | 121 | /** {@inheritDoc} */ |
| 122 | @Override |
| 123 | public void testLogSaved( |
| 124 | String dataName, LogDataType dataType, InputStreamSource dataStream, LogFile logFile) { |
| 125 | for (ITestInvocationListener listener : getListeners()) { |
| 126 | try { |
| 127 | // Forward the testLogSaved event to ILogSaverListener |
| 128 | if (listener instanceof ILogSaverListener) { |
| 129 | ((ILogSaverListener) listener) |
| 130 | .testLogSaved(dataName, dataType, dataStream, logFile); |
| 131 | } |
| 132 | } catch (Exception e) { |
| 133 | CLog.e("Exception while invoking %s#testLogSaved", listener.getClass().getName()); |
| 134 | CLog.e(e); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Julien Desprez | e6b756b | 2018-04-16 10:56:38 -0700 | [diff] [blame] | 139 | /** Only forward the testLog instead of saving the log first. */ |
| 140 | public void testLogForward( |
| 141 | String dataName, LogDataType dataType, InputStreamSource dataStream) { |
| 142 | for (ITestInvocationListener listener : getListeners()) { |
| 143 | if (listener instanceof LogSaverResultForwarder) { |
| 144 | // If the listener is a log saver, we should simply forward the testLog not save |
| 145 | // again. |
| 146 | ((LogSaverResultForwarder) listener).testLogForward(dataName, dataType, dataStream); |
| 147 | } else { |
| 148 | try { |
| 149 | listener.testLog(dataName, dataType, dataStream); |
| 150 | } catch (RuntimeException e) { |
| 151 | CLog.e( |
| 152 | "RuntimeException while invoking %s#testLog", |
| 153 | listener.getClass().getName()); |
| 154 | CLog.e(e); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** {@inheritDoc} */ |
| 161 | @Override |
| 162 | public void logAssociation(String dataName, LogFile logFile) { |
| 163 | for (ITestInvocationListener listener : getListeners()) { |
| 164 | try { |
| 165 | // Forward the logAssociation call |
| 166 | if (listener instanceof ILogSaverListener) { |
| 167 | ((ILogSaverListener) listener).logAssociation(dataName, logFile); |
| 168 | } |
| 169 | } catch (RuntimeException e) { |
| 170 | CLog.e("Failed to provide the log association"); |
| 171 | CLog.e(e); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
jdesprez | 5c27266 | 2017-11-28 02:42:27 -0800 | [diff] [blame] | 176 | /** |
| 177 | * Copy the build info from the shard builds to the main build in the original invocation |
| 178 | * context. |
| 179 | * |
| 180 | * @param main the original {@link IInvocationContext} from the main invocation. |
| 181 | * @param shardContexts the list of {@link IInvocationContext}s, one for each shard invocation. |
| 182 | */ |
| 183 | private void copyShardBuildInfoToMain( |
| 184 | IInvocationContext main, List<IInvocationContext> shardContexts) { |
| 185 | for (IInvocationContext shard : shardContexts) { |
| 186 | for (String deviceName : shard.getDeviceConfigNames()) { |
| 187 | IBuildInfo shardBuild = shard.getBuildInfo(deviceName); |
| 188 | IBuildInfo mainBuild = main.getBuildInfo(deviceName); |
| 189 | if (mainBuild != null) { |
| 190 | for (Entry<String, String> entry : shardBuild.getBuildAttributes().entrySet()) { |
| 191 | mainBuild.addBuildAttribute(entry.getKey(), entry.getValue()); |
| 192 | } |
| 193 | } else { |
| 194 | // Should not happen |
| 195 | CLog.e( |
| 196 | "Found a device '%s' in shard configuration but not in parent configuration.", |
| 197 | deviceName); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 202 | } |