<commit_before>// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.package remote

package storage

import (
	"container/heap"
	"strings"

	"github.com/prometheus/prometheus/pkg/labels"
)

type fanout struct {
	storages []Storage
}

// NewFanout returns a new fan-out Storage, which proxies reads and writes
// through to multiple underlying storages.
func NewFanout(storages ...Storage) Storage {
	return &fanout{
		storages: storages,
	}
}

func (f *fanout) Querier(mint, maxt int64) (Querier, error) {
	queriers := mergeQuerier{
		queriers: make([]Querier, 0, len(f.storages)),
	}
	for _, storage := range f.storages {
		querier, err := storage.Querier(mint, maxt)
		if err != nil {
			queriers.Close()
			return nil, err
		}
		queriers.queriers = append(queriers.queriers, querier)
	}
	return &queriers, nil
}

func (f *fanout) Appender() (Appender, error) {
	appenders := make([]Appender, 0, len(f.storages))
	for _, storage := range f.storages {
		appender, err := storage.Appender()
		if err != nil {
			return nil, err
		}
		appenders = append(appenders, appender)
	}
	return &fanoutAppender{
		appenders: appenders,
	}, nil
}

// Close closes the storage and all its underlying resources.
func (f *fanout) Close() error {
	// TODO return multiple errors?
	var lastErr error
	for _, storage := range f.storages {
		if err := storage.Close(); err != nil {
			lastErr = err
		}
	}
	return lastErr
}

// fanoutAppender implements Appender.
type fanoutAppender struct {
	appenders []Appender
}

func (f *fanoutAppender) Add(l labels.Labels, t int64, v float64) (string, error) {
	for _, appender := range f.appenders {
		if _, err := appender.Add(l, t, v); err != nil {
			return "", err
		}
	}
	return "", nil
}

func (f *fanoutAppender) AddFast(l labels.Labels, ref string, t int64, v float64) error {
	for _, appender := range f.appenders {
		if err := appender.AddFast(l, ref, t, v); err != nil {
			return err
		}
	}
	return nil
}

func (f *fanoutAppender) Commit() error {
	for _, appender := range f.appenders {
		if err := appender.Commit(); err != nil {
			return err
		}
	}
	return nil
}

func (f *fanoutAppender) Rollback() error {
	for _, appender := range f.appenders {
		if err := appender.Rollback(); err != nil {
			return err
		}
	}
	return nil
}

// mergeQuerier implements Querier.
type mergeQuerier struct {
	queriers []Querier
}<commit_msg>Introduce 'primary' storage in fanout, and have Add return the ref from the primary.<commit_after>// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.package remote

package storage

import (
	"container/heap"
	"strings"

	"github.com/prometheus/common/log"
	"github.com/prometheus/prometheus/pkg/labels"
)

type fanout struct {
	primary     Storage
	secondaries []Storage
}

// NewFanout returns a new fan-out Storage, which proxies reads and writes
// through to multiple underlying storages.
func NewFanout(primary Storage, secondaries ...Storage) Storage {
	return &fanout{
		primary:     primary,
		secondaries: secondaries,
	}
}

func (f *fanout) Querier(mint, maxt int64) (Querier, error) {
	queriers := mergeQuerier{
		queriers: make([]Querier, 0, 1+len(f.secondaries)),
	}

	// Add primary querier
	querier, err := f.primary.Querier(mint, maxt)
	if err != nil {
		return nil, err
	}
	queriers.queriers = append(queriers.queriers, querier)

	// Add secondary queriers
	for _, storage := range f.secondaries {
		querier, err := storage.Querier(mint, maxt)
		if err != nil {
			queriers.Close()
			return nil, err
		}
		queriers.queriers = append(queriers.queriers, querier)
	}
	return &queriers, nil
}

func (f *fanout) Appender() (Appender, error) {
	primary, err := f.primary.Appender()
	if err != nil {
		return nil, err
	}

	secondaries := make([]Appender, 0, len(f.secondaries))
	for _, storage := range f.secondaries {
		appender, err := storage.Appender()
		if err != nil {
			return nil, err
		}
		secondaries = append(secondaries, appender)
	}
	return &fanoutAppender{
		primary:     primary,
		secondaries: secondaries,
	}, nil
}

// Close closes the storage and all its underlying resources.
func (f *fanout) Close() error {
	if err := f.primary.Close(); err != nil {
		return err
	}

	// TODO return multiple errors?
	var lastErr error
	for _, storage := range f.secondaries {
		if err := storage.Close(); err != nil {
			lastErr = err
		}
	}
	return lastErr
}

// fanoutAppender implements Appender.
type fanoutAppender struct {
	primary     Appender
	secondaries []Appender
}

func (f *fanoutAppender) Add(l labels.Labels, t int64, v float64) (string, error) {
	ref, err := f.primary.Add(l, t, v)
	if err != nil {
		return ref, err
	}

	for _, appender := range f.secondaries {
		if _, err := appender.Add(l, t, v); err != nil {
			return "", err
		}
	}
	return ref, nil
}

func (f *fanoutAppender) AddFast(l labels.Labels, ref string, t int64, v float64) error {
	if err := f.primary.AddFast(l, ref, t, v); err != nil {
		return err
	}

	for _, appender := range f.secondaries {
		if _, err := appender.Add(l, t, v); err != nil {
			return err
		}
	}
	return nil
}

func (f *fanoutAppender) Commit() (err error) {
	err = f.primary.Commit()

	for _, appender := range f.secondaries {
		if err == nil {
			err = appender.Commit()
		} else {
			if rollbackErr := appender.Rollback(); rollbackErr != nil {
				log.Errorf("Squashed rollback error on commit: %v", rollbackErr)
			}
		}
	}
	return
}

func (f *fanoutAppender) Rollback() (err error) {
	err = f.primary.Rollback()

	for _, appender := range f.secondaries {
		rollbackErr := appender.Rollback()
		if err == nil {
			err = rollbackErr
		} else if rollbackErr != nil {
			log.Errorf("Squashed rollback error on rollback: %v", rollbackErr)
		}
	}
	return nil
}

// mergeQuerier implements Querier.
type mergeQuerier struct {
	queriers []Querier
}
{'fraction_non_alphanumeric': 0.12090313182811362, 'fraction_numerical': 0.0059723233794610345, 'mean_word_length': 3.544010589013898, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7441366', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2276, 'n_words': 759}