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