|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package accord.coordinate; |
| 20 | + |
| 21 | +import java.util.function.BiConsumer; |
| 22 | + |
| 23 | +import accord.local.Commands; |
| 24 | +import accord.local.Node; |
| 25 | +import accord.local.SafeCommand; |
| 26 | +import accord.local.SafeCommandStore; |
| 27 | +import accord.primitives.Known; |
| 28 | +import accord.messages.CheckStatus.CheckStatusOk; |
| 29 | +import accord.messages.CheckStatus.IncludeInfo; |
| 30 | +import accord.primitives.WithQuorum; |
| 31 | +import accord.primitives.Participants; |
| 32 | +import accord.primitives.Route; |
| 33 | +import accord.primitives.TxnId; |
| 34 | +import accord.utils.MapReduceConsume; |
| 35 | + |
| 36 | +import static accord.coordinate.Infer.InvalidIf.NotKnownToBeInvalid; |
| 37 | +import static accord.coordinate.Infer.InvalidateAndCallback.locallyInvalidateAndCallback; |
| 38 | +import static accord.local.CommandStores.*; |
| 39 | +import static accord.primitives.Known.Nothing; |
| 40 | +import static accord.primitives.WithQuorum.HasQuorum; |
| 41 | + |
| 42 | +/** |
| 43 | + * Find some Route for a txnId using some known participants |
| 44 | + */ |
| 45 | +public class FetchSomeRoute extends CheckShards<Participants<?>> |
| 46 | +{ |
| 47 | + final LatentStoreSelector reportTo; |
| 48 | + final BiConsumer<Route<?>, Throwable> callback; |
| 49 | + FetchSomeRoute(Node node, TxnId txnId, Infer.InvalidIf invalidIf, Participants<?> contactable, LatentStoreSelector reportTo, BiConsumer<Route<?>, Throwable> callback) |
| 50 | + { |
| 51 | + super(node, txnId, contactable, IncludeInfo.Route, null, invalidIf); |
| 52 | + this.reportTo = reportTo; |
| 53 | + this.callback = callback; |
| 54 | + } |
| 55 | + |
| 56 | + public static void fetchSomeRoute(Node node, TxnId txnId, Infer.InvalidIf invalidIf, Participants<?> unseekables, LatentStoreSelector reportTo, BiConsumer<Route<?>, Throwable> callback) |
| 57 | + { |
| 58 | + if (!node.topology().hasEpoch(txnId.epoch())) |
| 59 | + { |
| 60 | + node.withEpochAtLeast(txnId.epoch(), callback, () -> fetchSomeRoute(node, txnId, invalidIf, unseekables, reportTo, callback)); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + FetchSomeRoute fetchSomeRoute = new FetchSomeRoute(node, txnId, invalidIf, unseekables, reportTo, callback); |
| 65 | + fetchSomeRoute.start(); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + public static void fetchSomeRoute(Node node, TxnId txnId, Participants<?> contactable, BiConsumer<Route<?>, Throwable> callback) |
| 70 | + { |
| 71 | + fetchSomeRoute(node, txnId, contactable, LatentStoreSelector.standard(), callback); |
| 72 | + } |
| 73 | + |
| 74 | + public static void fetchSomeRoute(Node node, TxnId txnId, Participants<?> contactable, LatentStoreSelector reportTo, BiConsumer<Route<?>, Throwable> callback) |
| 75 | + { |
| 76 | + fetchSomeRoute(node, txnId, NotKnownToBeInvalid, contactable, reportTo, callback); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected boolean isSufficient(CheckStatusOk ok) |
| 81 | + { |
| 82 | + return ok.route != null; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected void onDone(Success success, Throwable failure) |
| 87 | + { |
| 88 | + if (failure != null) callback.accept(null, failure); |
| 89 | + else |
| 90 | + { |
| 91 | + final Route<?> route = merged == null ? null : merged.route; |
| 92 | + if (route == null) |
| 93 | + { |
| 94 | + Known known = Nothing; |
| 95 | + if (merged != null) |
| 96 | + known = merged.finish(query, query, query, success.withQuorum, previouslyKnownToBeInvalidIf).knownFor(txnId, query, query); |
| 97 | + reportRouteNotFound(node, success.withQuorum, known, txnId, query, reportTo, callback); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + StoreSelector selector = reportTo.refine(txnId, null, query); |
| 102 | + node.mapReduceConsumeLocal(txnId, selector, new MapReduceConsume<>() |
| 103 | + { |
| 104 | + @Override |
| 105 | + public void accept(Object result, Throwable failure) |
| 106 | + { |
| 107 | + callback.accept(route, null); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Object apply(SafeCommandStore safeStore) |
| 112 | + { |
| 113 | + SafeCommand safeCommand = safeStore.ifInitialised(txnId); |
| 114 | + if (safeCommand != null) |
| 115 | + Commands.updateRoute(safeStore, safeCommand, route); |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Object reduce(Object o1, Object o2) |
| 121 | + { |
| 122 | + return null; |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static void reportRouteNotFound(Node node, WithQuorum withQuorum, Known found, TxnId txnId, Participants<?> participants, LatentStoreSelector reportTo, BiConsumer<Route<?>, Throwable> callback) |
| 130 | + { |
| 131 | + switch (found.outcome()) |
| 132 | + { |
| 133 | + default: throw new AssertionError("Unknown outcome: " + found.outcome()); |
| 134 | + case Abort: |
| 135 | + locallyInvalidateAndCallback(node, txnId, reportTo.refine(txnId, null, participants), participants, null, callback); |
| 136 | + break; |
| 137 | + |
| 138 | + case Unknown: |
| 139 | + if (withQuorum == HasQuorum && found.canProposeInvalidation()) |
| 140 | + { |
| 141 | + Invalidate.invalidate(node, txnId, participants, false, reportTo, (outcome, throwable) -> callback.accept(null, throwable)); |
| 142 | + break; |
| 143 | + } |
| 144 | + case Erased: |
| 145 | + case WasApply: |
| 146 | + case Apply: |
| 147 | + callback.accept(null, null); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments