View Javadoc
1   /*
2    * Copyright (C) 2016, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.ketch;
12  
13  import static java.util.concurrent.TimeUnit.DAYS;
14  import static java.util.concurrent.TimeUnit.HOURS;
15  import static java.util.concurrent.TimeUnit.MILLISECONDS;
16  import static java.util.concurrent.TimeUnit.MINUTES;
17  import static java.util.concurrent.TimeUnit.SECONDS;
18  import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_COMMIT;
19  import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_SPEED;
20  import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_TYPE;
21  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_REMOTE;
22  
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.concurrent.TimeUnit;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  import org.eclipse.jgit.internal.ketch.KetchReplica.CommitMethod;
31  import org.eclipse.jgit.internal.ketch.KetchReplica.CommitSpeed;
32  import org.eclipse.jgit.internal.ketch.KetchReplica.Participation;
33  import org.eclipse.jgit.lib.Config;
34  
35  /**
36   * Configures a {@link org.eclipse.jgit.internal.ketch.KetchReplica}.
37   */
38  public class ReplicaConfig {
39  	/**
40  	 * Read a configuration from a config block.
41  	 *
42  	 * @param cfg
43  	 *            configuration to read.
44  	 * @param name
45  	 *            of the replica being configured.
46  	 * @return replica configuration for {@code name}.
47  	 */
48  	public static ReplicaConfig newFromConfig(Config cfg, String name) {
49  		return new ReplicaConfig().fromConfig(cfg, name);
50  	}
51  
52  	private Participation participation = Participation.FULL;
53  	private CommitMethod commitMethod = CommitMethod.ALL_REFS;
54  	private CommitSpeed commitSpeed = CommitSpeed.BATCHED;
55  	private long minRetry = SECONDS.toMillis(5);
56  	private long maxRetry = MINUTES.toMillis(1);
57  
58  	/**
59  	 * Get participation of the replica in the system.
60  	 *
61  	 * @return participation of the replica in the system.
62  	 */
63  	public Participation getParticipation() {
64  		return participation;
65  	}
66  
67  	/**
68  	 * Get how Ketch should apply committed changes.
69  	 *
70  	 * @return how Ketch should apply committed changes.
71  	 */
72  	public CommitMethod getCommitMethod() {
73  		return commitMethod;
74  	}
75  
76  	/**
77  	 * Get how quickly should Ketch commit.
78  	 *
79  	 * @return how quickly should Ketch commit.
80  	 */
81  	public CommitSpeed getCommitSpeed() {
82  		return commitSpeed;
83  	}
84  
85  	/**
86  	 * Returns the minimum wait delay before retrying a failure.
87  	 *
88  	 * @param unit
89  	 *            to get retry delay in.
90  	 * @return minimum delay before retrying a failure.
91  	 */
92  	public long getMinRetry(TimeUnit unit) {
93  		return unit.convert(minRetry, MILLISECONDS);
94  	}
95  
96  	/**
97  	 * Returns the maximum wait delay before retrying a failure.
98  	 *
99  	 * @param unit
100 	 *            to get retry delay in.
101 	 * @return maximum delay before retrying a failure.
102 	 */
103 	public long getMaxRetry(TimeUnit unit) {
104 		return unit.convert(maxRetry, MILLISECONDS);
105 	}
106 
107 	/**
108 	 * Update the configuration from a config block.
109 	 *
110 	 * @param cfg
111 	 *            configuration to read.
112 	 * @param name
113 	 *            of the replica being configured.
114 	 * @return {@code this}
115 	 */
116 	public ReplicaConfig fromConfig(Config cfg, String name) {
117 		participation = cfg.getEnum(
118 				CONFIG_KEY_REMOTE, name, CONFIG_KEY_TYPE,
119 				participation);
120 		commitMethod = cfg.getEnum(
121 				CONFIG_KEY_REMOTE, name, CONFIG_KEY_COMMIT,
122 				commitMethod);
123 		commitSpeed = cfg.getEnum(
124 				CONFIG_KEY_REMOTE, name, CONFIG_KEY_SPEED,
125 				commitSpeed);
126 		minRetry = getMillis(cfg, name, "ketch-minRetry", minRetry); //$NON-NLS-1$
127 		maxRetry = getMillis(cfg, name, "ketch-maxRetry", maxRetry); //$NON-NLS-1$
128 		return this;
129 	}
130 
131 	private static long getMillis(Config cfg, String name, String key,
132 			long defaultValue) {
133 		String valStr = cfg.getString(CONFIG_KEY_REMOTE, name, key);
134 		if (valStr == null) {
135 			return defaultValue;
136 		}
137 
138 		valStr = valStr.trim();
139 		if (valStr.isEmpty()) {
140 			return defaultValue;
141 		}
142 
143 		Matcher m = UnitMap.PATTERN.matcher(valStr);
144 		if (!m.matches()) {
145 			return defaultValue;
146 		}
147 
148 		String digits = m.group(1);
149 		String unitName = m.group(2).trim();
150 		TimeUnit unit = UnitMap.UNITS.get(unitName);
151 		if (unit == null) {
152 			return defaultValue;
153 		}
154 
155 		try {
156 			if (digits.indexOf('.') == -1) {
157 				return unit.toMillis(Long.parseLong(digits));
158 			}
159 
160 			double val = Double.parseDouble(digits);
161 			return (long) (val * unit.toMillis(1));
162 		} catch (NumberFormatException nfe) {
163 			return defaultValue;
164 		}
165 	}
166 
167 	static class UnitMap {
168 		static final Pattern PATTERN = Pattern
169 				.compile("^([1-9][0-9]*(?:\\.[0-9]*)?)\\s*(.*)$"); //$NON-NLS-1$
170 
171 		static final Map<String, TimeUnit> UNITS;
172 
173 		static {
174 			Map<String, TimeUnit> m = new HashMap<>();
175 			TimeUnit u = MILLISECONDS;
176 			m.put("", u); //$NON-NLS-1$
177 			m.put("ms", u); //$NON-NLS-1$
178 			m.put("millis", u); //$NON-NLS-1$
179 			m.put("millisecond", u); //$NON-NLS-1$
180 			m.put("milliseconds", u); //$NON-NLS-1$
181 
182 			u = SECONDS;
183 			m.put("s", u); //$NON-NLS-1$
184 			m.put("sec", u); //$NON-NLS-1$
185 			m.put("secs", u); //$NON-NLS-1$
186 			m.put("second", u); //$NON-NLS-1$
187 			m.put("seconds", u); //$NON-NLS-1$
188 
189 			u = MINUTES;
190 			m.put("m", u); //$NON-NLS-1$
191 			m.put("min", u); //$NON-NLS-1$
192 			m.put("mins", u); //$NON-NLS-1$
193 			m.put("minute", u); //$NON-NLS-1$
194 			m.put("minutes", u); //$NON-NLS-1$
195 
196 			u = HOURS;
197 			m.put("h", u); //$NON-NLS-1$
198 			m.put("hr", u); //$NON-NLS-1$
199 			m.put("hrs", u); //$NON-NLS-1$
200 			m.put("hour", u); //$NON-NLS-1$
201 			m.put("hours", u); //$NON-NLS-1$
202 
203 			u = DAYS;
204 			m.put("d", u); //$NON-NLS-1$
205 			m.put("day", u); //$NON-NLS-1$
206 			m.put("days", u); //$NON-NLS-1$
207 
208 			UNITS = Collections.unmodifiableMap(m);
209 		}
210 
211 		private UnitMap() {
212 		}
213 	}
214 }