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