1 /* 2 * Copyright (C) 2016, Google Inc. 3 * and other copyright owners as documented in the project's IP log. 4 * 5 * This program and the accompanying materials are made available 6 * under the terms of the Eclipse Distribution License v1.0 which 7 * accompanies this distribution, is reproduced below, and is 8 * available at http://www.eclipse.org/org/documents/edl-v10.php 9 * 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials provided 22 * with the distribution. 23 * 24 * - Neither the name of the Eclipse Foundation, Inc. nor the 25 * names of its contributors may be used to endorse or promote 26 * products derived from this software without specific prior 27 * written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 44 package org.eclipse.jgit.internal.ketch; 45 46 import java.util.Collection; 47 import java.util.Map; 48 49 import org.eclipse.jgit.annotations.Nullable; 50 import org.eclipse.jgit.lib.Ref; 51 import org.eclipse.jgit.lib.Repository; 52 import org.eclipse.jgit.transport.ReceiveCommand; 53 54 /** 55 * A push request sending objects to a replica, and its result. 56 * <p> 57 * Implementors of {@link KetchReplica} must populate the command result fields, 58 * {@link #setRefs(Map)}, and call one of 59 * {@link #setException(Repository, Throwable)} or {@link #done(Repository)} to 60 * finish processing. 61 */ 62 public class ReplicaPushRequest { 63 private final KetchReplica replica; 64 private final Collection<ReceiveCommand> commands; 65 private Map<String, Ref> refs; 66 private Throwable exception; 67 private boolean notified; 68 69 /** 70 * Construct a new push request for a replica. 71 * 72 * @param replica 73 * the replica being pushed to. 74 * @param commands 75 * commands to be executed. 76 */ 77 public ReplicaPushRequest(KetchReplica replica, 78 Collection<ReceiveCommand> commands) { 79 this.replica = replica; 80 this.commands = commands; 81 } 82 83 /** @return commands to be executed, and their results. */ 84 public Collection<ReceiveCommand> getCommands() { 85 return commands; 86 } 87 88 /** @return remote references, usually from the advertisement. */ 89 @Nullable 90 public Map<String, Ref> getRefs() { 91 return refs; 92 } 93 94 /** 95 * @param refs 96 * references observed from the replica. 97 */ 98 public void setRefs(Map<String, Ref> refs) { 99 this.refs = refs; 100 } 101 102 /** @return exception thrown, if any. */ 103 @Nullable 104 public Throwable getException() { 105 return exception; 106 } 107 108 /** 109 * Mark the request as crashing with a communication error. 110 * <p> 111 * This method may take significant time acquiring the leader lock and 112 * updating the Ketch state machine with the failure. 113 * 114 * @param repo 115 * local repository reference used by the push attempt. 116 * @param err 117 * exception thrown during communication. 118 */ 119 public void setException(@Nullable Repository repo, Throwable err) { 120 if (KetchReplica.log.isErrorEnabled()) { 121 KetchReplica.log.error(describe("failed"), err); //$NON-NLS-1$ 122 } 123 if (!notified) { 124 notified = true; 125 exception = err; 126 replica.afterPush(repo, this); 127 } 128 } 129 130 /** 131 * Mark the request as completed without exception. 132 * <p> 133 * This method may take significant time acquiring the leader lock and 134 * updating the Ketch state machine with results from this replica. 135 * 136 * @param repo 137 * local repository reference used by the push attempt. 138 */ 139 public void done(Repository repo) { 140 if (KetchReplica.log.isDebugEnabled()) { 141 KetchReplica.log.debug(describe("completed")); //$NON-NLS-1$ 142 } 143 if (!notified) { 144 notified = true; 145 replica.afterPush(repo, this); 146 } 147 } 148 149 private String describe(String heading) { 150 StringBuilder b = new StringBuilder(); 151 b.append("push to "); //$NON-NLS-1$ 152 b.append(replica.describeForLog()); 153 b.append(' ').append(heading).append(":\n"); //$NON-NLS-1$ 154 for (ReceiveCommand cmd : commands) { 155 b.append(String.format( 156 " %-12s %-12s %s %s", //$NON-NLS-1$ 157 LeaderSnapshot.str(cmd.getOldId()), 158 LeaderSnapshot.str(cmd.getNewId()), 159 cmd.getRefName(), 160 cmd.getResult())); 161 if (cmd.getMessage() != null) { 162 b.append(' ').append(cmd.getMessage()); 163 } 164 b.append('\n'); 165 } 166 return b.toString(); 167 } 168 }