1   /*
2    * Copyright 2019 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.server.internal.mirror;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import java.io.File;
21  import java.util.concurrent.CompletableFuture;
22  import java.util.concurrent.CompletionStage;
23  
24  import javax.annotation.Nullable;
25  
26  import com.google.common.base.MoreObjects;
27  
28  import com.linecorp.centraldogma.server.CentralDogmaConfig;
29  import com.linecorp.centraldogma.server.plugin.Plugin;
30  import com.linecorp.centraldogma.server.plugin.PluginContext;
31  import com.linecorp.centraldogma.server.plugin.PluginTarget;
32  
33  public final class DefaultMirroringServicePlugin implements Plugin {
34  
35      @Nullable
36      private volatile DefaultMirroringService mirroringService;
37  
38      @Override
39      public PluginTarget target() {
40          return PluginTarget.LEADER_ONLY;
41      }
42  
43      @Override
44      public synchronized CompletionStage<Void> start(PluginContext context) {
45          requireNonNull(context, "context");
46  
47          DefaultMirroringService mirroringService = this.mirroringService;
48          if (mirroringService == null) {
49              final CentralDogmaConfig cfg = context.config();
50              mirroringService = new DefaultMirroringService(new File(cfg.dataDir(), "_mirrors"),
51                                                             context.projectManager(),
52                                                             context.meterRegistry(),
53                                                             cfg.numMirroringThreads(),
54                                                             cfg.maxNumFilesPerMirror(),
55                                                             cfg.maxNumBytesPerMirror());
56              this.mirroringService = mirroringService;
57          }
58          mirroringService.start(context.commandExecutor());
59          return CompletableFuture.completedFuture(null);
60      }
61  
62      @Override
63      public synchronized CompletionStage<Void> stop(PluginContext context) {
64          final DefaultMirroringService mirroringService = this.mirroringService;
65          if (mirroringService != null && mirroringService.isStarted()) {
66              mirroringService.stop();
67          }
68          return CompletableFuture.completedFuture(null);
69      }
70  
71      @Override
72      public boolean isEnabled(CentralDogmaConfig config) {
73          return requireNonNull(config, "config").isMirroringEnabled();
74      }
75  
76      @Nullable
77      public DefaultMirroringService mirroringService() {
78          return mirroringService;
79      }
80  
81      @Override
82      public String toString() {
83          return MoreObjects.toStringHelper(this)
84                            .add("target", target())
85                            .toString();
86      }
87  }