ReplicaConfig.java

  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. package org.eclipse.jgit.internal.ketch;

  44. import static java.util.concurrent.TimeUnit.DAYS;
  45. import static java.util.concurrent.TimeUnit.HOURS;
  46. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  47. import static java.util.concurrent.TimeUnit.MINUTES;
  48. import static java.util.concurrent.TimeUnit.SECONDS;
  49. import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_COMMIT;
  50. import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_SPEED;
  51. import static org.eclipse.jgit.internal.ketch.KetchConstants.CONFIG_KEY_TYPE;
  52. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_REMOTE;

  53. import java.util.Collections;
  54. import java.util.HashMap;
  55. import java.util.Map;
  56. import java.util.concurrent.TimeUnit;
  57. import java.util.regex.Matcher;
  58. import java.util.regex.Pattern;

  59. import org.eclipse.jgit.internal.ketch.KetchReplica.CommitMethod;
  60. import org.eclipse.jgit.internal.ketch.KetchReplica.CommitSpeed;
  61. import org.eclipse.jgit.internal.ketch.KetchReplica.Participation;
  62. import org.eclipse.jgit.lib.Config;

  63. /**
  64.  * Configures a {@link org.eclipse.jgit.internal.ketch.KetchReplica}.
  65.  */
  66. public class ReplicaConfig {
  67.     /**
  68.      * Read a configuration from a config block.
  69.      *
  70.      * @param cfg
  71.      *            configuration to read.
  72.      * @param name
  73.      *            of the replica being configured.
  74.      * @return replica configuration for {@code name}.
  75.      */
  76.     public static ReplicaConfig newFromConfig(Config cfg, String name) {
  77.         return new ReplicaConfig().fromConfig(cfg, name);
  78.     }

  79.     private Participation participation = Participation.FULL;
  80.     private CommitMethod commitMethod = CommitMethod.ALL_REFS;
  81.     private CommitSpeed commitSpeed = CommitSpeed.BATCHED;
  82.     private long minRetry = SECONDS.toMillis(5);
  83.     private long maxRetry = MINUTES.toMillis(1);

  84.     /**
  85.      * Get participation of the replica in the system.
  86.      *
  87.      * @return participation of the replica in the system.
  88.      */
  89.     public Participation getParticipation() {
  90.         return participation;
  91.     }

  92.     /**
  93.      * Get how Ketch should apply committed changes.
  94.      *
  95.      * @return how Ketch should apply committed changes.
  96.      */
  97.     public CommitMethod getCommitMethod() {
  98.         return commitMethod;
  99.     }

  100.     /**
  101.      * Get how quickly should Ketch commit.
  102.      *
  103.      * @return how quickly should Ketch commit.
  104.      */
  105.     public CommitSpeed getCommitSpeed() {
  106.         return commitSpeed;
  107.     }

  108.     /**
  109.      * Returns the minimum wait delay before retrying a failure.
  110.      *
  111.      * @param unit
  112.      *            to get retry delay in.
  113.      * @return minimum delay before retrying a failure.
  114.      */
  115.     public long getMinRetry(TimeUnit unit) {
  116.         return unit.convert(minRetry, MILLISECONDS);
  117.     }

  118.     /**
  119.      * Returns the maximum wait delay before retrying a failure.
  120.      *
  121.      * @param unit
  122.      *            to get retry delay in.
  123.      * @return maximum delay before retrying a failure.
  124.      */
  125.     public long getMaxRetry(TimeUnit unit) {
  126.         return unit.convert(maxRetry, MILLISECONDS);
  127.     }

  128.     /**
  129.      * Update the configuration from a config block.
  130.      *
  131.      * @param cfg
  132.      *            configuration to read.
  133.      * @param name
  134.      *            of the replica being configured.
  135.      * @return {@code this}
  136.      */
  137.     public ReplicaConfig fromConfig(Config cfg, String name) {
  138.         participation = cfg.getEnum(
  139.                 CONFIG_KEY_REMOTE, name, CONFIG_KEY_TYPE,
  140.                 participation);
  141.         commitMethod = cfg.getEnum(
  142.                 CONFIG_KEY_REMOTE, name, CONFIG_KEY_COMMIT,
  143.                 commitMethod);
  144.         commitSpeed = cfg.getEnum(
  145.                 CONFIG_KEY_REMOTE, name, CONFIG_KEY_SPEED,
  146.                 commitSpeed);
  147.         minRetry = getMillis(cfg, name, "ketch-minRetry", minRetry); //$NON-NLS-1$
  148.         maxRetry = getMillis(cfg, name, "ketch-maxRetry", maxRetry); //$NON-NLS-1$
  149.         return this;
  150.     }

  151.     private static long getMillis(Config cfg, String name, String key,
  152.             long defaultValue) {
  153.         String valStr = cfg.getString(CONFIG_KEY_REMOTE, name, key);
  154.         if (valStr == null) {
  155.             return defaultValue;
  156.         }

  157.         valStr = valStr.trim();
  158.         if (valStr.isEmpty()) {
  159.             return defaultValue;
  160.         }

  161.         Matcher m = UnitMap.PATTERN.matcher(valStr);
  162.         if (!m.matches()) {
  163.             return defaultValue;
  164.         }

  165.         String digits = m.group(1);
  166.         String unitName = m.group(2).trim();
  167.         TimeUnit unit = UnitMap.UNITS.get(unitName);
  168.         if (unit == null) {
  169.             return defaultValue;
  170.         }

  171.         try {
  172.             if (digits.indexOf('.') == -1) {
  173.                 return unit.toMillis(Long.parseLong(digits));
  174.             }

  175.             double val = Double.parseDouble(digits);
  176.             return (long) (val * unit.toMillis(1));
  177.         } catch (NumberFormatException nfe) {
  178.             return defaultValue;
  179.         }
  180.     }

  181.     static class UnitMap {
  182.         static final Pattern PATTERN = Pattern
  183.                 .compile("^([1-9][0-9]*(?:\\.[0-9]*)?)\\s*(.*)$"); //$NON-NLS-1$

  184.         static final Map<String, TimeUnit> UNITS;

  185.         static {
  186.             Map<String, TimeUnit> m = new HashMap<>();
  187.             TimeUnit u = MILLISECONDS;
  188.             m.put("", u); //$NON-NLS-1$
  189.             m.put("ms", u); //$NON-NLS-1$
  190.             m.put("millis", u); //$NON-NLS-1$
  191.             m.put("millisecond", u); //$NON-NLS-1$
  192.             m.put("milliseconds", u); //$NON-NLS-1$

  193.             u = SECONDS;
  194.             m.put("s", u); //$NON-NLS-1$
  195.             m.put("sec", u); //$NON-NLS-1$
  196.             m.put("secs", u); //$NON-NLS-1$
  197.             m.put("second", u); //$NON-NLS-1$
  198.             m.put("seconds", u); //$NON-NLS-1$

  199.             u = MINUTES;
  200.             m.put("m", u); //$NON-NLS-1$
  201.             m.put("min", u); //$NON-NLS-1$
  202.             m.put("mins", u); //$NON-NLS-1$
  203.             m.put("minute", u); //$NON-NLS-1$
  204.             m.put("minutes", u); //$NON-NLS-1$

  205.             u = HOURS;
  206.             m.put("h", u); //$NON-NLS-1$
  207.             m.put("hr", u); //$NON-NLS-1$
  208.             m.put("hrs", u); //$NON-NLS-1$
  209.             m.put("hour", u); //$NON-NLS-1$
  210.             m.put("hours", u); //$NON-NLS-1$

  211.             u = DAYS;
  212.             m.put("d", u); //$NON-NLS-1$
  213.             m.put("day", u); //$NON-NLS-1$
  214.             m.put("days", u); //$NON-NLS-1$

  215.             UNITS = Collections.unmodifiableMap(m);
  216.         }

  217.         private UnitMap() {
  218.         }
  219.     }
  220. }