Exemple #1
0
Fichier : unit.go Projet : pulcy/j2
func (r *EtcdRegistry) storeOrGetUnitFile(u unit.UnitFile) (err error) {
	um := unitModel{
		Raw: u.String(),
	}

	val, err := marshal(um)
	if err != nil {
		return err
	}

	key := r.hashedUnitPath(u.Hash())
	opts := &etcd.SetOptions{
		PrevExist: etcd.PrevNoExist,
	}
	start := time.Now()
	_, err = r.kAPI.Set(context.Background(), key, val, opts)
	// unit is already stored
	if isEtcdError(err, etcd.ErrorCodeNodeExist) {
		// TODO(jonboulle): verify more here?
		err = nil
	}
	if err != nil {
		metrics.ReportRegistryOpFailure(metrics.Set)
		return
	}
	metrics.ReportRegistryOpSuccess(metrics.Set, start)
	return
}
Exemple #2
0
Fichier : unit.go Projet : pulcy/j2
// getUnitByHash retrieves from the Registry the Unit associated with the given Hash
func (r *EtcdRegistry) getUnitByHash(hash unit.Hash) *unit.UnitFile {
	key := r.hashedUnitPath(hash)
	opts := &etcd.GetOptions{
		Recursive: true,
	}
	start := time.Now()
	resp, err := r.kAPI.Get(context.Background(), key, opts)
	if err != nil {
		if isEtcdError(err, etcd.ErrorCodeKeyNotFound) {
			err = nil
		}
		metrics.ReportRegistryOpFailure(metrics.Get)
		return nil
	}
	metrics.ReportRegistryOpSuccess(metrics.Get, start)
	return r.unitFromEtcdNode(hash, resp.Node)
}
Exemple #3
0
Fichier : unit.go Projet : pulcy/j2
// getAllUnitsHashMap retrieves from the Registry all Units and returns a map of hash to UnitFile
func (r *EtcdRegistry) getAllUnitsHashMap() (map[string]*unit.UnitFile, error) {
	key := r.prefixed(unitPrefix)
	opts := &etcd.GetOptions{
		Recursive: true,
		Quorum:    true,
	}
	hashToUnit := map[string]*unit.UnitFile{}
	start := time.Now()
	resp, err := r.kAPI.Get(context.Background(), key, opts)
	if err != nil {
		metrics.ReportRegistryOpFailure(metrics.GetAll)
		return nil, err
	}
	metrics.ReportRegistryOpSuccess(metrics.GetAll, start)

	for _, node := range resp.Node.Nodes {
		parts := strings.Split(node.Key, "/")
		if len(parts) == 0 {
			log.Errorf("key '%v' doesn't have enough parts", node.Key)
			continue
		}
		stringHash := parts[len(parts)-1]
		hash, err := unit.HashFromHexString(stringHash)
		if err != nil {
			log.Errorf("failed to get Hash for key '%v' with stringHash '%v': %v", node.Key, stringHash, err)
			continue
		}
		unit := r.unitFromEtcdNode(hash, node)
		if unit == nil {
			continue
		}
		hashToUnit[stringHash] = unit
	}

	return hashToUnit, nil
}