github.com/uber/kraken@v0.1.4/lib/persistedretry/tagreplication/executor.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package tagreplication
    15  
    16  import (
    17  	"fmt"
    18  	"time"
    19  
    20  	"github.com/uber/kraken/build-index/tagclient"
    21  	"github.com/uber/kraken/lib/persistedretry"
    22  	"github.com/uber/kraken/origin/blobclient"
    23  
    24  	"github.com/uber-go/tally"
    25  )
    26  
    27  // Executor executes tag replication tasks.
    28  type Executor struct {
    29  	stats             tally.Scope
    30  	originCluster     blobclient.ClusterClient
    31  	tagClientProvider tagclient.Provider
    32  }
    33  
    34  // NewExecutor creates a new Executor.
    35  func NewExecutor(
    36  	stats tally.Scope,
    37  	originCluster blobclient.ClusterClient,
    38  	tagClientProvider tagclient.Provider) *Executor {
    39  
    40  	stats = stats.Tagged(map[string]string{
    41  		"module": "tagreplicationexecutor",
    42  	})
    43  
    44  	return &Executor{stats, originCluster, tagClientProvider}
    45  }
    46  
    47  // Name returns the executor name.
    48  func (e *Executor) Name() string {
    49  	return "tagreplication"
    50  }
    51  
    52  // Exec replicates a tag's blob dependencies to the task's remote origin
    53  // cluster, then replicates the tag to the remote build-index.
    54  func (e *Executor) Exec(r persistedretry.Task) error {
    55  	t := r.(*Task)
    56  	start := time.Now()
    57  	remoteTagClient := e.tagClientProvider.Provide(t.Destination)
    58  
    59  	if ok, err := remoteTagClient.Has(t.Tag); err == nil && ok {
    60  		// Remote index already has the tag, therefore dependencies have already
    61  		// been replicated, and the remote has also replicated the tag. No-op.
    62  		return nil
    63  	}
    64  
    65  	remoteOrigin, err := remoteTagClient.Origin()
    66  	if err != nil {
    67  		return fmt.Errorf("lookup remote origin cluster: %s", err)
    68  	}
    69  	for _, d := range t.Dependencies {
    70  		if err := e.originCluster.ReplicateToRemote(t.Tag, d, remoteOrigin); err != nil {
    71  			return fmt.Errorf("origin cluster replicate: %s", err)
    72  		}
    73  	}
    74  
    75  	// Put tag and triggers replication on the remote client.
    76  	// Replication will call Exec n^2 times but some will return early
    77  	// if remote has the tag already.
    78  	if err := remoteTagClient.PutAndReplicate(t.Tag, t.Digest); err != nil {
    79  		return fmt.Errorf("put and replicate tag: %s", err)
    80  	}
    81  
    82  	// We don't want to time noops nor errors.
    83  	e.stats.Timer("replicate").Record(time.Since(start))
    84  	e.stats.Timer("lifetime").Record(time.Since(t.CreatedAt))
    85  
    86  	return nil
    87  }