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 java.util.concurrent.TimeUnit.DAYS;
47  import static java.util.concurrent.TimeUnit.HOURS;
48  import static java.util.concurrent.TimeUnit.MILLISECONDS;
49  import static java.util.concurrent.TimeUnit.MINUTES;
50  import static java.util.concurrent.TimeUnit.SECONDS;
51  import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_COMMIT;
52  import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_SPEED;
53  import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_TYPE;
54  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_REMOTE;
55  
56  import java.util.Collections;
57  import java.util.HashMap;
58  import java.util.Map;
59  import java.util.concurrent.TimeUnit;
60  import java.util.regex.Matcher;
61  import java.util.regex.Pattern;
62  
63  import org.eclipse.jgit.internal.ketch.KetchReplica.CommitMethod;
64  import org.eclipse.jgit.internal.ketch.KetchReplica.CommitSpeed;
65  import org.eclipse.jgit.internal.ketch.KetchReplica.Participation;
66  import org.eclipse.jgit.lib.Config;
67  
68  /**
69   * Configures a {@link org.eclipse.jgit.internal.ketch.KetchReplica}.
70   */
71  public class ReplicaConfig {
72  	/**
73  	 * Read a configuration from a config block.
74  	 *
75  	 * @param cfg
76  	 *            configuration to read.
77  	 * @param name
78  	 *            of the replica being configured.
79  	 * @return replica configuration for {@code name}.
80  	 */
81  	public static ReplicaConfig newFromConfig(Config cfg, String name) {
82  		return new ReplicaConfig().fromConfig(cfg, name);
83  	}
84  
85  	private Participation participation = Participation.FULL;
86  	private CommitMethod commitMethod = CommitMethod.ALL_REFS;
87  	private CommitSpeed commitSpeed = CommitSpeed.BATCHED;
88  	private long minRetry = SECONDS.toMillis(5);
89  	private long maxRetry = MINUTES.toMillis(1);
90  
91  	/**
92  	 * Get participation of the replica in the system.
93  	 *
94  	 * @return participation of the replica in the system.
95  	 */
96  	public Participation getParticipation() {
97  		return participation;
98  	}
99  
100 	/**
101 	 * Get how Ketch should apply committed changes.
102 	 *
103 	 * @return how Ketch should apply committed changes.
104 	 */
105 	public CommitMethod getCommitMethod() {
106 		return commitMethod;
107 	}
108 
109 	/**
110 	 * Get how quickly should Ketch commit.
111 	 *
112 	 * @return how quickly should Ketch commit.
113 	 */
114 	public CommitSpeed getCommitSpeed() {
115 		return commitSpeed;
116 	}
117 
118 	/**
119 	 * Returns the minimum wait delay before retrying a failure.
120 	 *
121 	 * @param unit
122 	 *            to get retry delay in.
123 	 * @return minimum delay before retrying a failure.
124 	 */
125 	public long getMinRetry(TimeUnit unit) {
126 		return unit.convert(minRetry, MILLISECONDS);
127 	}
128 
129 	/**
130 	 * Returns the maximum wait delay before retrying a failure.
131 	 *
132 	 * @param unit
133 	 *            to get retry delay in.
134 	 * @return maximum delay before retrying a failure.
135 	 */
136 	public long getMaxRetry(TimeUnit unit) {
137 		return unit.convert(maxRetry, MILLISECONDS);
138 	}
139 
140 	/**
141 	 * Update the configuration from a config block.
142 	 *
143 	 * @param cfg
144 	 *            configuration to read.
145 	 * @param name
146 	 *            of the replica being configured.
147 	 * @return {@code this}
148 	 */
149 	public ReplicaConfig fromConfig(Config cfg, String name) {
150 		participation = cfg.getEnum(
151 				CONFIG_KEY_REMOTE, name, CONFIG_KEY_TYPE,
152 				participation);
153 		commitMethod = cfg.getEnum(
154 				CONFIG_KEY_REMOTE, name, CONFIG_KEY_COMMIT,
155 				commitMethod);
156 		commitSpeed = cfg.getEnum(
157 				CONFIG_KEY_REMOTE, name, CONFIG_KEY_SPEED,
158 				commitSpeed);
159 		minRetry = getMillis(cfg, name, "ketch-minRetry", minRetry); //$NON-NLS-1$
160 		maxRetry = getMillis(cfg, name, "ketch-maxRetry", maxRetry); //$NON-NLS-1$
161 		return this;
162 	}
163 
164 	private static long getMillis(Config cfg, String name, String key,
165 			long defaultValue) {
166 		String valStr = cfg.getString(CONFIG_KEY_REMOTE, name, key);
167 		if (valStr == null) {
168 			return defaultValue;
169 		}
170 
171 		valStr = valStr.trim();
172 		if (valStr.isEmpty()) {
173 			return defaultValue;
174 		}
175 
176 		Matcher m = UnitMap.PATTERN.matcher(valStr);
177 		if (!m.matches()) {
178 			return defaultValue;
179 		}
180 
181 		String digits = m.group(1);
182 		String unitName = m.group(2).trim();
183 		TimeUnit unit = UnitMap.UNITS.get(unitName);
184 		if (unit == null) {
185 			return defaultValue;
186 		}
187 
188 		try {
189 			if (digits.indexOf('.') == -1) {
190 				return unit.toMillis(Long.parseLong(digits));
191 			}
192 
193 			double val = Double.parseDouble(digits);
194 			return (long) (val * unit.toMillis(1));
195 		} catch (NumberFormatException nfe) {
196 			return defaultValue;
197 		}
198 	}
199 
200 	static class UnitMap {
201 		static final Pattern PATTERN = Pattern
202 				.compile("^([1-9][0-9]*(?:\\.[0-9]*)?)\\s*(.*)$"); //$NON-NLS-1$
203 
204 		static final Map<String, TimeUnit> UNITS;
205 
206 		static {
207 			Map<String, TimeUnit> m = new HashMap<>();
208 			TimeUnit u = MILLISECONDS;
209 			m.put("", u); //$NON-NLS-1$
210 			m.put("ms", u); //$NON-NLS-1$
211 			m.put("millis", u); //$NON-NLS-1$
212 			m.put("millisecond", u); //$NON-NLS-1$
213 			m.put("milliseconds", u); //$NON-NLS-1$
214 
215 			u = SECONDS;
216 			m.put("s", u); //$NON-NLS-1$
217 			m.put("sec", u); //$NON-NLS-1$
218 			m.put("secs", u); //$NON-NLS-1$
219 			m.put("second", u); //$NON-NLS-1$
220 			m.put("seconds", u); //$NON-NLS-1$
221 
222 			u = MINUTES;
223 			m.put("m", u); //$NON-NLS-1$
224 			m.put("min", u); //$NON-NLS-1$
225 			m.put("mins", u); //$NON-NLS-1$
226 			m.put("minute", u); //$NON-NLS-1$
227 			m.put("minutes", u); //$NON-NLS-1$
228 
229 			u = HOURS;
230 			m.put("h", u); //$NON-NLS-1$
231 			m.put("hr", u); //$NON-NLS-1$
232 			m.put("hrs", u); //$NON-NLS-1$
233 			m.put("hour", u); //$NON-NLS-1$
234 			m.put("hours", u); //$NON-NLS-1$
235 
236 			u = DAYS;
237 			m.put("d", u); //$NON-NLS-1$
238 			m.put("day", u); //$NON-NLS-1$
239 			m.put("days", u); //$NON-NLS-1$
240 
241 			UNITS = Collections.unmodifiableMap(m);
242 		}
243 
244 		private UnitMap() {
245 		}
246 	}
247 }