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 org.eclipse.jgit.internal.ketch.KetchReplica} must 58 * populate the command result fields, {@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 /** 84 * Get commands to be executed, and their results. 85 * 86 * @return commands to be executed, and their results. 87 */ 88 public Collection<ReceiveCommand> getCommands() { 89 return commands; 90 } 91 92 /** 93 * Get remote references, usually from the advertisement. 94 * 95 * @return remote references, usually from the advertisement. 96 */ 97 @Nullable 98 public Map<String, Ref> getRefs() { 99 return refs; 100 } 101 102 /** 103 * Set references observed from the replica. 104 * 105 * @param refs 106 * references observed from the replica. 107 */ 108 public void setRefs(Map<String, Ref> refs) { 109 this.refs = refs; 110 } 111 112 /** 113 * Get exception thrown, if any. 114 * 115 * @return exception thrown, if any. 116 */ 117 @Nullable 118 public Throwable getException() { 119 return exception; 120 } 121 122 /** 123 * Mark the request as crashing with a communication error. 124 * <p> 125 * This method may take significant time acquiring the leader lock and 126 * updating the Ketch state machine with the failure. 127 * 128 * @param repo 129 * local repository reference used by the push attempt. 130 * @param err 131 * exception thrown during communication. 132 */ 133 public void setException(@Nullable Repository repo, Throwable err) { 134 if (KetchReplica.log.isErrorEnabled()) { 135 KetchReplica.log.error(describe("failed"), err); //$NON-NLS-1$ 136 } 137 if (!notified) { 138 notified = true; 139 exception = err; 140 replica.afterPush(repo, this); 141 } 142 } 143 144 /** 145 * Mark the request as completed without exception. 146 * <p> 147 * This method may take significant time acquiring the leader lock and 148 * updating the Ketch state machine with results from this replica. 149 * 150 * @param repo 151 * local repository reference used by the push attempt. 152 */ 153 public void done(Repository repo) { 154 if (KetchReplica.log.isDebugEnabled()) { 155 KetchReplica.log.debug(describe("completed")); //$NON-NLS-1$ 156 } 157 if (!notified) { 158 notified = true; 159 replica.afterPush(repo, this); 160 } 161 } 162 163 private String describe(String heading) { 164 StringBuilder b = new StringBuilder(); 165 b.append("push to "); //$NON-NLS-1$ 166 b.append(replica.describeForLog()); 167 b.append(' ').append(heading).append(":\n"); //$NON-NLS-1$ 168 for (ReceiveCommand cmd : commands) { 169 b.append(String.format( 170 " %-12s %-12s %s %s", //$NON-NLS-1$ 171 LeaderSnapshot.str(cmd.getOldId()), 172 LeaderSnapshot.str(cmd.getNewId()), 173 cmd.getRefName(), 174 cmd.getResult())); 175 if (cmd.getMessage() != null) { 176 b.append(' ').append(cmd.getMessage()); 177 } 178 b.append('\n'); 179 } 180 return b.toString(); 181 } 182 }