View Javadoc
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 static org.eclipse.jgit.internal.ketch.KetchReplica.State.AHEAD;
47  import static org.eclipse.jgit.internal.ketch.KetchReplica.State.DIVERGENT;
48  import static org.eclipse.jgit.internal.ketch.KetchReplica.State.LAGGING;
49  import static org.eclipse.jgit.internal.ketch.KetchReplica.State.UNKNOWN;
50  import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
51  
52  import java.io.IOException;
53  import java.util.Collections;
54  import java.util.Map;
55  
56  import org.eclipse.jgit.errors.MissingObjectException;
57  import org.eclipse.jgit.lib.AnyObjectId;
58  import org.eclipse.jgit.lib.ObjectId;
59  import org.eclipse.jgit.lib.Ref;
60  import org.eclipse.jgit.lib.Repository;
61  import org.eclipse.jgit.revwalk.RevCommit;
62  import org.eclipse.jgit.revwalk.RevWalk;
63  import org.eclipse.jgit.transport.ReceiveCommand;
64  
65  /**
66   * A helper to check if a {@link KetchReplica} is ahead or behind the leader.
67   */
68  class LagCheck implements AutoCloseable {
69  	private final KetchReplica replica;
70  	private final Repository repo;
71  	private RevWalk rw;
72  	private ObjectId remoteId;
73  
74  	LagCheck(KetchReplica replica, Repository repo) {
75  		this.replica = replica;
76  		this.repo = repo;
77  		initRevWalk();
78  	}
79  
80  	private void initRevWalk() {
81  		if (rw != null) {
82  			rw.close();
83  		}
84  
85  		rw = new RevWalk(repo);
86  		rw.setRetainBody(false);
87  	}
88  
89  	@Override
90  	public void close() {
91  		if (rw != null) {
92  			rw.close();
93  			rw = null;
94  		}
95  	}
96  
97  	ObjectId getRemoteId() {
98  		return remoteId;
99  	}
100 
101 	KetchReplica.State check(ObjectId acceptId, ReceiveCommand acceptCmd) {
102 		remoteId = acceptId;
103 		if (remoteId == null) {
104 			// Nothing advertised by the replica, value is unknown.
105 			return UNKNOWN;
106 		}
107 
108 		if (AnyObjectId.equals(remoteId, ObjectId.zeroId())) {
109 			// Replica does not have the txnAccepted reference.
110 			return LAGGING;
111 		}
112 
113 		try {
114 			RevCommit remote;
115 			try {
116 				remote = parseRemoteCommit(acceptCmd.getRefName());
117 			} catch (RefGoneException gone) {
118 				// Replica does not have the txnAccepted reference.
119 				return LAGGING;
120 			} catch (MissingObjectException notFound) {
121 				// Local repository does not know this commit so it cannot
122 				// be including the replica's log.
123 				return DIVERGENT;
124 			}
125 
126 			RevCommit head = rw.parseCommit(acceptCmd.getNewId());
127 			if (rw.isMergedInto(remote, head)) {
128 				return LAGGING;
129 			}
130 
131 			// TODO(sop) Check term to see if my leader was deposed.
132 			if (rw.isMergedInto(head, remote)) {
133 				return AHEAD;
134 			} else {
135 				return DIVERGENT;
136 			}
137 		} catch (IOException err) {
138 			KetchReplica.log.error(String.format(
139 					"Cannot compare %s", //$NON-NLS-1$
140 					acceptCmd.getRefName()), err);
141 			return UNKNOWN;
142 		}
143 	}
144 
145 	private RevCommit parseRemoteCommit(String refName)
146 			throws IOException, MissingObjectException, RefGoneException {
147 		try {
148 			return rw.parseCommit(remoteId);
149 		} catch (MissingObjectException notLocal) {
150 			// Fall through and try to acquire the object by fetching it.
151 		}
152 
153 		ReplicaFetchRequest fetch = new ReplicaFetchRequest(
154 				Collections.singleton(refName),
155 				Collections.<ObjectId> emptySet());
156 		try {
157 			replica.blockingFetch(repo, fetch);
158 		} catch (IOException fetchErr) {
159 			KetchReplica.log.error(String.format(
160 					"Cannot fetch %s (%s) from %s", //$NON-NLS-1$
161 					remoteId.abbreviate(8).name(), refName,
162 					replica.describeForLog()), fetchErr);
163 			throw new MissingObjectException(remoteId, OBJ_COMMIT);
164 		}
165 
166 		Map<String, Ref> adv = fetch.getRefs();
167 		if (adv == null) {
168 			throw new MissingObjectException(remoteId, OBJ_COMMIT);
169 		}
170 
171 		Ref ref = adv.get(refName);
172 		if (ref == null || ref.getObjectId() == null) {
173 			throw new RefGoneException();
174 		}
175 
176 		initRevWalk();
177 		remoteId = ref.getObjectId();
178 		return rw.parseCommit(remoteId);
179 	}
180 
181 	private static class RefGoneException extends Exception {
182 		private static final long serialVersionUID = 1L;
183 	}
184 }