|
39 | 39 | import org.elasticsearch.common.io.stream.StreamInput;
|
40 | 40 | import org.elasticsearch.common.io.stream.StreamOutput;
|
41 | 41 | import org.elasticsearch.common.settings.Settings;
|
| 42 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
42 | 43 | import org.elasticsearch.gateway.WriteStateException;
|
43 | 44 | import org.elasticsearch.index.shard.IndexShard;
|
44 | 45 | import org.elasticsearch.index.shard.IndexShardClosedException;
|
@@ -99,40 +100,45 @@ protected void doExecute(Task parentTask, Request request, ActionListener<Respon
|
99 | 100 |
|
100 | 101 | final void sync(ShardId shardId, String primaryAllocationId, long primaryTerm, RetentionLeases retentionLeases,
|
101 | 102 | ActionListener<ReplicationResponse> listener) {
|
102 |
| - final Request request = new Request(shardId, retentionLeases); |
103 |
| - final ReplicationTask task = (ReplicationTask) taskManager.register("transport", "retention_lease_sync", request); |
104 |
| - transportService.sendChildRequest(clusterService.localNode(), transportPrimaryAction, |
105 |
| - new ConcreteShardRequest<>(request, primaryAllocationId, primaryTerm), |
106 |
| - task, |
107 |
| - transportOptions, |
108 |
| - new TransportResponseHandler<ReplicationResponse>() { |
109 |
| - @Override |
110 |
| - public ReplicationResponse read(StreamInput in) throws IOException { |
111 |
| - return newResponseInstance(in); |
112 |
| - } |
113 |
| - |
114 |
| - @Override |
115 |
| - public String executor() { |
116 |
| - return ThreadPool.Names.SAME; |
117 |
| - } |
118 |
| - |
119 |
| - @Override |
120 |
| - public void handleResponse(ReplicationResponse response) { |
121 |
| - task.setPhase("finished"); |
122 |
| - taskManager.unregister(task); |
123 |
| - listener.onResponse(response); |
124 |
| - } |
125 |
| - |
126 |
| - @Override |
127 |
| - public void handleException(TransportException e) { |
128 |
| - if (ExceptionsHelper.unwrap(e, AlreadyClosedException.class, IndexShardClosedException.class) == null) { |
129 |
| - getLogger().warn(new ParameterizedMessage("{} retention lease sync failed", shardId), e); |
| 103 | + final ThreadContext threadContext = threadPool.getThreadContext(); |
| 104 | + try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { |
| 105 | + // we have to execute under the system context so that if security is enabled the sync is authorized |
| 106 | + threadContext.markAsSystemContext(); |
| 107 | + final Request request = new Request(shardId, retentionLeases); |
| 108 | + final ReplicationTask task = (ReplicationTask) taskManager.register("transport", "retention_lease_sync", request); |
| 109 | + transportService.sendChildRequest(clusterService.localNode(), transportPrimaryAction, |
| 110 | + new ConcreteShardRequest<>(request, primaryAllocationId, primaryTerm), |
| 111 | + task, |
| 112 | + transportOptions, |
| 113 | + new TransportResponseHandler<ReplicationResponse>() { |
| 114 | + @Override |
| 115 | + public ReplicationResponse read(StreamInput in) throws IOException { |
| 116 | + return newResponseInstance(in); |
130 | 117 | }
|
131 |
| - task.setPhase("finished"); |
132 |
| - taskManager.unregister(task); |
133 |
| - listener.onFailure(e); |
134 |
| - } |
135 |
| - }); |
| 118 | + |
| 119 | + @Override |
| 120 | + public String executor() { |
| 121 | + return ThreadPool.Names.SAME; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void handleResponse(ReplicationResponse response) { |
| 126 | + task.setPhase("finished"); |
| 127 | + taskManager.unregister(task); |
| 128 | + listener.onResponse(response); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void handleException(TransportException e) { |
| 133 | + if (ExceptionsHelper.unwrap(e, AlreadyClosedException.class, IndexShardClosedException.class) == null) { |
| 134 | + getLogger().warn(new ParameterizedMessage("{} retention lease sync failed", shardId), e); |
| 135 | + } |
| 136 | + task.setPhase("finished"); |
| 137 | + taskManager.unregister(task); |
| 138 | + listener.onFailure(e); |
| 139 | + } |
| 140 | + }); |
| 141 | + } |
136 | 142 | }
|
137 | 143 |
|
138 | 144 | @Override
|
|
0 commit comments