text:
package server

import (
	"context"
	"fmt"
	"math/rand"
	"net"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/m3db/m3/src/aggregator/server"
	clusterclient "github.com/m3db/m3/src/cluster/client"
	etcdclient "github.com/m3db/m3/src/cluster/client/etcd"
	"github.com/m3db/m3/src/cluster/kv"
	memcluster "github.com/m3db/m3/src/cluster/mem"
	handleroptions3 "github.com/m3db/m3/src/cluster/placementhandler/handleroptions"
	"github.com/m3db/m3/src/cmd/services/m3aggregator/serve"
	"github.com/m3db/m3/src/cmd/services/m3coordinator/downsample"
	"github.com/m3db/m3/src/cmd/services/m3coordinator/ingest"
	ingestcarbon "github.com/m3db/m3/src/cmd/services/m3coordinator/ingest/carbon"
	dbconfig "github.com/m3db/m3/src/cmd/services/m3dbnode/config"
	"github.com/m3db/m3/src/cmd/services/m3query/config"
	"github.com/m3db/m3/src/dbnode/client"
	"github.com/m3db/m3/src/dbnode/encoding"
	"github.com/m3db/m3/src/query/api/v1/handler/prometheus/handleroptions"
	"github.com/m3db/m3/src/query/api/v1/httpd"
	"github.com/m3db/m3/src/query/api/v1/options"
	m3dbcluster "github.com/m3db/m3/src/query/cluster/m3db"
	"github.com/m3db/m3/src/query/executor"
	graphite "github.com/m3db/m3/src/query/graphite/storage"
	"github.com/m3db/m3/src/query/models"
	"github.com/m3db/m3/src/query/parser/promql"
	"github.com/m3db/m3/src/query/policy/filter"
	"github.com/m3db/m3/src/query/pools"
	"github.com/m3db/m3/src/query/promqlengine"
	tsdbremote "github.com/m3db/m3/src/query/remote"
	"github.com/m3db/m3/src/query/storage"
	"github.com/m3db/m3/src/query/storage/fanout"
	"github.com/m3db/m3/src/query/storage/m3"
	"github.com/m3db/m3/src/query/storage/m3/consolidators"
	"github.com/m3db/m3/src/query/storage/promremote"
	"github.com/m3db/m3/src/query/storage/remote"
	"github.com/m3db/m3/src/query/stores/m3db"
	"github.com/m3db/m3/src/x/clock"
	xconfig "github.com/m3db/m3/src/x/config"
	"github.com/m3db/m3/src/x/instrument"
	xio "github.com/m3db/m3/src/x/io"
	xnet "github.com/m3db/m3/src/x/net"
	xos "github.com/m3db/m3/src/x/os"
	"github.com/m3db/m3/src/x/pool"
	"github.com/m3db/m3/src/x/serialize"
	xserver "github.com/m3db/m3/src/x/server"
	xsync "github.com/m3db/m3/src/x/sync"

	"github.com/go-kit/kit/log"
	kitlogzap "github.com/go-kit/kit/log/zap"
	"github.com/opentracing/opentracing-go"
	"github.com/pkg/errors"
	extprom "github.com/prometheus/client_golang/prometheus"
	prometheuspromql "github.com/prometheus/prometheus/promql"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"golang.org/x/net/http2"
	"golang.org/x/net/http2/h2c"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

const (
	serviceName        = "m3query"
	cpuProfileDuration = 5 * time.Second
)

var (
	defaultCarbonIngesterWorkerPoolSize = 1024
	defaultPerCPUMultiProcess           = 0.5
)

type cleanupFn func() error

// RunOptions provides options for running the server
// with backwards compatibility if only solely adding fields.
type RunOptions struct {
	// Config is an alternate way to provide configuration and will be used
	// instead of parsing ConfigFile if ConfigFile is not specified.
	Config config.Configuration

	// DBConfig is the local M3DB config when running embedded.
	DBConfig *dbconfig.DBConfiguration

	// DBClient is the local M3DB client when running embedded.
	DBClient <-chan client.Client

	// ClusterClient is the local M3DB cluster client when running embedded.
	ClusterClient <-chan clusterclient.Client

	// InterruptCh is a programmatic interrupt channel to supply to
	// interrupt and shutdown the server.
	InterruptCh <-chan error

	// ShutdownCh is an optional channel to supply if interested in receiving
	// a notification that the server has shutdown.
	ShutdownCh chan<- struct{}

	// ListenerCh is a programmatic channel to receive the server listener
	// on once it has opened.
	ListenerCh chan<- net.Listener

	// M3MsgListenerCh is a programmatic channel to receive the M3Msg server
	// listener on once it has opened.
	M3MsgListenerCh chan<- net.Listener

	// DownsamplerReadyCh is a programmatic channel to receive the downsampler
	// ready signal once it is open.
	DownsamplerReadyCh chan<- struct{}

	// LocalSessionReadyCh is a programmatic channel to receive the
	// local DB session ready signal once it is open.
	LocalSessionReadyCh chan struct{}

	// InstrumentOptionsReadyCh is a programmatic channel to receive a set of
	// instrument options and metric reporters that is delivered when
	// constructed.
	InstrumentOptionsReadyCh chan<- InstrumentOptionsReady

	// ClockOptions is an optional clock to use instead of the default one.
	ClockOptions clock.Options

	// CustomHandlerOptions creates custom handler options.
	CustomHandlerOptions CustomHandlerOptionsFn

	// CustomPromQLParseFunction is a custom PromQL parsing function.
	CustomPromQLParseFunction promql.ParseFn

	// ApplyCustomTSDBOptions is a transform that allows for custom tsdb options.
	ApplyCustomTSDBOptions CustomTSDBOptionsFn

	// BackendStorageTransform is a custom backend storage transform.
	BackendStorageTransform BackendStorageTransformFn

	// AggregatorServerOptions are server options for aggregator.
	AggregatorServerOptions []server.AdminOption

	// CustomBuildTags are additional tags to be added to the instrument build
	// reporter.
	CustomBuildTags map[string]string

	// ApplyCustomRuleStore provides an option to swap the backend used for the rule stores.
	ApplyCustomRuleStore downsample.CustomRuleStoreFn
}

// InstrumentOptionsReady is a set of instrument options
// and metric reporters that is delivered when constructed.
type InstrumentOptionsReady struct {
	InstrumentOptions instrument.Options
	MetricsReporters  instrument.MetricsConfigurationReporters
}

// CustomTSDBOptionsFn is a transformation function for TSDB Options.
type CustomTSDBOptionsFn func(m3.Options, instrument.Options) (m3.Options, error)

// CustomHandlerOptionsFn is a factory for options.CustomHandlerOptions.
type CustomHandlerOptionsFn func(instrument.Options) (options.CustomHandlerOptions, error)

// BackendStorageTransformFn is a transformation function for backend storage.
type BackendStorageTransformFn func(
	storage.Storage,
	m3.Options,
	instrument.Options,
) (storage.Storage, error)

// RunResult returns metadata about the process run.
type RunResult struct {
	MultiProcessRun               bool
	MultiProcessIsParentCleanExit bool
}

// Run runs the server programmatically given a filename for the configuration file.
func Run(runOpts RunOptions) RunResult {
	rand.Seed(time.Now().UnixNano())

	var (
		cfg          = runOpts.Config
		listenerOpts = xnet.NewListenerOptions()
		runResult    RunResult
	)

	logger, err := cfg.LoggingOrDefault().BuildLogger()
	if err != nil {
		// NB(r): Use fmt.Fprintf(os.Stderr, ...) to avoid etcd.SetGlobals()
		// sending stdlib "log" to black hole. Don't remove unless with good reason.
		fmt.Fprintf(os.Stderr, "unable to create logger: %v", err)
		os.Exit(1)
	}

	// NB(nate): Register shutdown notification defer function first so that
	// it's the last defer to fire before terminating. This allows other defer methods
	// that clean up resources to execute first.
	if runOpts.ShutdownCh != nil {
		defer func() {
			select {
			case runOpts.ShutdownCh <- struct{}{}:
				break
			default:
				logger.Warn("could not send shutdown notification as channel was full")
			}
		}()
	}

	interruptOpts := xos.NewInterruptOptions()
	if runOpts.InterruptCh != nil {
		interruptOpts.InterruptCh = runOpts.InterruptCh
	}
	intWatchCancel := xos.WatchForInterrupt(logger, interruptOpts)
	defer intWatchCancel()

	defer logger.Sync()

	cfg.Debug.SetRuntimeValues(logger)

	xconfig.WarnOnDeprecation(cfg, logger)

	var commonLabels map[string]string
	if cfg.MultiProcess.Enabled {
		// Mark as a multi-process run result.
		runResult.MultiProcessRun = true

		// Execute multi-process parent spawn or child setup code path.
		multiProcessRunResult, err := multiProcessRun(cfg, logger, listenerOpts)
		if err != nil {
			logger = logger.With(zap.String("processID", multiProcessProcessID()))
			logger.Fatal("failed to run", zap.Error(err))
		}
		if multiProcessRunResult.isParentCleanExit {
			// Parent process clean exit.
			runResult.MultiProcessIsParentCleanExit = true
			return runResult
		}

		cfg = multiProcessRunResult.cfg
		logger = multiProcessRunResult.logger
		listenerOpts = multiProcessRunResult.listenerOpts
		commonLabels = multiProcessRunResult.commonLabels
	}

	prometheusEngineRegistry := extprom.NewRegistry()
	scope, closer, reporters, err := cfg.MetricsOrDefault().NewRootScopeAndReporters(
		instrument.NewRootScopeAndReportersOptions{
			PrometheusExternalRegistries: []instrument.PrometheusExternalRegistry{
				{
					Registry: prometheusEngineRegistry,
					SubScope: "coordinator_prometheus_engine",
				},
			},
			PrometheusOnError: func(err error) {
				// NB(r): Required otherwise collisions when registering metrics will
				// cause a panic.
				logger.Error("register metric error", zap.Error(err))
			},
			CommonLabels: commonLabels,
		})
	if err != nil {
		logger.Fatal("could not connect to metrics", zap.Error(err))
	}

	tracer, traceCloser, err := cfg.Tracing.NewTracer(serviceName, scope, logger)
	if err != nil {
		logger.Fatal("could not initialize tracing", zap.Error(err))
	}

	defer traceCloser.Close()

	if _, ok := tracer.(opentracing.NoopTracer); ok {
		logger.Info("tracing disabled for m3query; set `tracing.backend` to enable")
	}

	opentracing.SetGlobalTracer(tracer)

	clockOpts := clock.NewOptions()
	if runOpts.ClockOptions != nil {
		clockOpts = runOpts.ClockOptions
	}

	instrumentOptions := instrument.NewOptions().
		SetMetricsScope(scope).
		SetLogger(logger).
		SetTracer(tracer).
		SetCustomBuildTags(runOpts.CustomBuildTags)

	if runOpts.InstrumentOptionsReadyCh != nil {
		runOpts.InstrumentOptionsReadyCh <- InstrumentOptionsReady{
			InstrumentOptions: instrumentOptions,
			MetricsReporters:  reporters,
		}
	}

	// Close metrics scope
	defer func() {
		logger.Info("closing metrics scope")
		if err := closer.Close(); err != nil {
			logger.Error("unable to close metrics scope", zap.Error(err))
		}
	}()

	buildReporter := instrument.NewBuildReporter(instrumentOptions)
	if err := buildReporter.Start(); err != nil {
		logger.Fatal("could not start build reporter", zap.Error(err))
	}

	defer buildReporter.Stop()

	storageRestrictByTags, _, err := cfg.Query.RestrictTagsAsStorageRestrictByTag()
	if err != nil {
		logger.Fatal("could not parse query restrict tags config", zap.Error(err))
	}

	timeout := cfg.Query.TimeoutOrDefault()
	if runOpts.DBConfig != nil &&
		runOpts.DBConfig.Client.FetchTimeout != nil &&
		*runOpts.DBConfig.Client.FetchTimeout > timeout {
		timeout = *runOpts.DBConfig.Client.FetchTimeout
	}

	fetchOptsBuilderLimitsOpts := cfg.Limits.PerQuery.AsFetchOptionsBuilderLimitsOptions()
	fetchOptsBuilder, err := handleroptions.NewFetchOptionsBuilder(
		handleroptions.FetchOptionsBuilderOptions{
			Limits:        fetchOptsBuilderLimitsOpts,
			RestrictByTag: storageRestrictByTags,
			Timeout:       timeout,
		})
	if err != nil {
		logger.Fatal("could not set fetch options parser", zap.Error(err))
	}

	var (
		clusterNamespacesWatcher = m3.NewClusterNamespacesWatcher()
		backendStorage           storage.Storage
		clusterClient            clusterclient.Client
		downsampler              downsample.Downsampler
		queryCtxOpts             = models.QueryContextOptions{
			LimitMaxTimeseries:             fetchOptsBuilderLimitsOpts.SeriesLimit,
			LimitMaxDocs:                   fetchOptsBuilderLimitsOpts.DocsLimit,
			LimitMaxReturnedSeries:         fetchOptsBuilderLimitsOpts.ReturnedSeriesLimit,
			LimitMaxReturnedDatapoints:     fetchOptsBuilderLimitsOpts.ReturnedDatapointsLimit,
			LimitMaxReturnedSeriesMetadata: fetchOptsBuilderLimitsOpts.ReturnedSeriesMetadataLimit,
			RequireExhaustive:              fetchOptsBuilderLimitsOpts.RequireExhaustive,
		}

		matchOptions = consolidators.MatchOptions{
			MatchType: cfg.Query.ConsolidationConfiguration.MatchType,
		}
	)
	defer clusterNamespacesWatcher.Close()

	tagOptions, err := config.TagOptionsFromConfig(cfg.TagOptions)
	if err != nil {
		logger.Fatal("could not create tag options", zap.Error(err))
	}

	lookbackDuration, err := cfg.LookbackDurationOrDefault()
	if err != nil {
		logger.Fatal("error validating LookbackDuration", zap.Error(err))
	}
	cfg.LookbackDuration = &lookbackDuration

	promConvertOptions := cfg.Query.Prometheus.ConvertOptionsOrDefault()

	readWorkerPool, writeWorkerPool, err := pools.BuildWorkerPools(
		instrumentOptions,
		cfg.ReadWorkerPool,
		cfg.WriteWorkerPoolOrDefault(),
		scope)
	if err != nil {
		logger.Fatal("could not create worker pools", zap.Error(err))
	}

	var (
		encodingOpts    = encoding.NewOptions()
		m3dbClusters    m3.Clusters
		m3dbPoolWrapper *pools.PoolWrapper
	)

	tsdbOpts := m3.NewOptions(encodingOpts).
		SetTagOptions(tagOptions).
		SetLookbackDuration(lookbackDuration).
		SetConsolidationFunc(consolidators.TakeLast).
		SetReadWorkerPool(readWorkerPool).
		SetWriteWorkerPool(writeWorkerPool).
		SetSeriesConsolidationMatchOptions(matchOptions).
		SetPromConvertOptions(promConvertOptions)

	if runOpts.ApplyCustomTSDBOptions != nil {
		tsdbOpts, err = runOpts.ApplyCustomTSDBOptions(tsdbOpts, instrumentOptions)
		if err != nil {
			logger.Fatal("could not apply ApplyCustomTSDBOptions", zap.Error(err))
		}
	}

	serveOptions := serve.NewOptions(instrumentOptions)
	for i, transform := range runOpts.AggregatorServerOptions {
		if opts, err := transform(serveOptions); err != nil {
			logger.Fatal("could not apply transform",
				zap.Int("index", i), zap.Error(err))
		} else {
			serveOptions = opts
		}
	}

	rwOpts := serveOptions.RWOptions()
	switch cfg.Backend {
	case config.GRPCStorageType:
		// For grpc backend, we need to setup only the grpc client and a storage
		// accompanying that client.
		poolWrapper := pools.NewPoolsWrapper(
			pools.BuildIteratorPools(encodingOpts, pools.BuildIteratorPoolsOptions{}))
		remoteOpts := config.RemoteOptionsFromConfig(cfg.RPC)
		remotes, enabled, err := remoteClient(poolWrapper, remoteOpts,
			tsdbOpts, instrumentOptions)
		if err != nil {
			logger.Fatal("unable to setup grpc backend", zap.Error(err))
		}
		if !enabled {
			logger.Fatal("need remote clients for grpc backend")
		}

		var (
			r = filter.AllowAll
			w = filter.AllowAll
			c = filter.CompleteTagsAllowAll
		)

		backendStorage = fanout.NewStorage(remotes, r, w, c,
			tagOptions, tsdbOpts, instrumentOptions)
		logger.Info("setup grpc backend")

	case config.NoopEtcdStorageType:
		backendStorage = storage.NewNoopStorage()
		etcd := cfg.ClusterManagement.Etcd

		if etcd == nil || len(etcd.ETCDClusters) == 0 {
			logger.Fatal("must specify cluster management config and at least one etcd cluster")
		}

		opts := etcd.NewOptions()
		clusterClient, err = etcdclient.NewConfigServiceClient(opts)
		if err != nil {
			logger.Fatal("error constructing etcd client", zap.Error(err))
		}
		logger.Info("setup noop storage backend with etcd")

	// Empty backend defaults to M3DB.
	case "", config.M3DBStorageType:
		// For m3db backend, we need to make connections to the m3db cluster
		// which generates a session and use the storage with the session.
		m3dbClusters, m3dbPoolWrapper, err = initClusters(cfg, runOpts.DBConfig,
			clusterNamespacesWatcher, runOpts.DBClient, encodingOpts,
			runOpts.LocalSessionReadyCh, instrumentOptions,
			tsdbOpts.CustomAdminOptions())
		if err != nil {
			logger.Fatal("unable to init clusters", zap.Error(err))
		}

		var cleanup cleanupFn
		backendStorage, cleanup, err = newM3DBStorage(
			cfg, m3dbClusters, m3dbPoolWrapper, queryCtxOpts, tsdbOpts, instrumentOptions,
		)
		if err != nil {
			logger.Fatal("unable to setup m3db backend", zap.Error(err))
		}

		defer cleanup()

		etcdConfig, err := resolveEtcdForM3DB(cfg)
		if err != nil {
			logger.Fatal("unable to resolve etcd config for m3db backend", zap.Error(err))
		}

		logger.Info("configuring downsampler to use with aggregated cluster namespaces",
			zap.Int("numAggregatedClusterNamespaces", len(m3dbClusters.ClusterNamespaces())))

		downsampler, clusterClient, err = newDownsamplerAsync(cfg.Downsample, etcdConfig, backendStorage,
			clusterNamespacesWatcher, tsdbOpts.TagOptions(), clockOpts, instrumentOptions, rwOpts, runOpts,
			interruptOpts,
		)
		if err != nil {
			var interruptErr *xos.InterruptError
			if errors.As(err, &interruptErr) {
				logger.Warn("interrupt received. closing server", zap.Error(err))
				return runResult
			}
			logger.Fatal("unable to setup downsampler for m3db backend", zap.Error(err))
		}
	case config.PromRemoteStorageType:
		opts, err := promremote.NewOptions(cfg.PrometheusRemoteBackend, scope, instrumentOptions.Logger())
		if err != nil {
			logger.Fatal("invalid configuration", zap.Error(err))
		}
		backendStorage, err = promremote.NewStorage(opts)
		if err != nil {
			logger.Fatal("unable to setup prom remote backend", zap.Error(err))
		}
		defer func() {
			if err := backendStorage.Close(); err != nil {
				logger.Error("error when closing storage", zap.Error(err))
			}
		}()

		logger.Info("configuring downsampler to use with aggregated namespaces",
			zap.Int("numAggregatedClusterNamespaces", opts.Namespaces().NumAggregatedClusterNamespaces()))
		err = clusterNamespacesWatcher.Update(opts.Namespaces())
		if err != nil {
			logger.Fatal("unable to update namespaces", zap.Error(err))
		}

		downsampler, clusterClient, err = newDownsamplerAsync(cfg.Downsample, cfg.ClusterManagement.Etcd, backendStorage,
			clusterNamespacesWatcher, tsdbOpts.TagOptions(), clockOpts, instrumentOptions, rwOpts, runOpts,
			interruptOpts,
		)
		if err != nil {
			logger.Fatal("unable to setup downsampler for prom remote backend", zap.Error(err))
		}
	default:
		logger.Fatal("unrecognized backend", zap.String("backend", string(cfg.Backend)))
	}

	if fn := runOpts.BackendStorageTransform; fn != nil {
		backendStorage, err = fn(backendStorage, tsdbOpts, instrumentOptions)
		if err != nil {
			logger.Fatal("could not apply BackendStorageTransform", zap.Error(err))
		}
	}

	engineOpts := executor.NewEngineOptions().
		SetStore(backendStorage).
		SetLookbackDuration(*cfg.LookbackDuration).
		SetInstrumentOptions(instrumentOptions.
			SetMetricsScope(instrumentOptions.MetricsScope().SubScope("engine")))
	if fn := runOpts.CustomPromQLParseFunction; fn != nil {
		engineOpts = engineOpts.
			SetParseOptions(engineOpts.ParseOptions().SetParseFn(fn))
	}

	engine := executor.NewEngine(engineOpts)
	downsamplerAndWriter, err := newDownsamplerAndWriter(
		backendStorage,
		downsampler,
		cfg.WriteWorkerPoolOrDefault(),
		instrumentOptions,
	)
	if err != nil {
		logger.Fatal("unable to create new downsampler and writer", zap.Error(err))
	}

	var serviceOptionDefaults []handleroptions3.ServiceOptionsDefault
	if dbCfg := runOpts.DBConfig; dbCfg != nil {
		hostID, err := dbCfg.HostIDOrDefault().Resolve()
		if err != nil {
			logger.Fatal("could not resolve hostID",
				zap.Error(err))
		}

		discoveryCfg := dbCfg.DiscoveryOrDefault()
		envCfg, err := discoveryCfg.EnvironmentConfig(hostID)
		if err != nil {
			logger.Fatal("could not get env config from discovery config",
				zap.Error(err))
		}

		cluster, err := envCfg.Services.SyncCluster()
		if err != nil {
			logger.Fatal("could not resolve embedded db cluster info",
				zap.Error(err))
		}
		if svcCfg := cluster.Service; svcCfg != nil {
			serviceOptionDefaults = append(serviceOptionDefaults,
				handleroptions3.WithDefaultServiceEnvironment(svcCfg.Env))
			serviceOptionDefaults = append(serviceOptionDefaults,
				handleroptions3.WithDefaultServiceZone(svcCfg.Zone))
		}
	}

	var (
		graphiteFindFetchOptsBuilder   = fetchOptsBuilder
		graphiteRenderFetchOptsBuilder = fetchOptsBuilder
		graphiteStorageOpts            graphite.M3WrappedStorageOptions
	)
	if cfg.Carbon != nil {
		graphiteStorageOpts = graphite.M3WrappedStorageOptions{
			AggregateNamespacesAllData:                 cfg.Carbon.AggregateNamespacesAllData,
			ShiftTimeStart:                             cfg.Carbon.ShiftTimeStart,
			ShiftTimeEnd:                               cfg.Carbon.ShiftTimeEnd,
			ShiftStepsStart:                            cfg.Carbon.ShiftStepsStart,
			ShiftStepsEnd:                              cfg.Carbon.ShiftStepsEnd,
			ShiftStepsStartWhenAtResolutionBoundary:    cfg.Carbon.ShiftStepsStartWhenAtResolutionBoundary,
			ShiftStepsEndWhenAtResolutionBoundary:      cfg.Carbon.ShiftStepsEndWhenAtResolutionBoundary,
			ShiftStepsEndWhenStartAtResolutionBoundary: cfg.Carbon.ShiftStepsEndWhenStartAtResolutionBoundary,
			ShiftStepsStartWhenEndAtResolutionBoundary: cfg.Carbon.ShiftStepsStartWhenEndAtResolutionBoundary,
			RenderPartialStart:                         cfg.Carbon.RenderPartialStart,
			RenderPartialEnd:                           cfg.Carbon.RenderPartialEnd,
			RenderSeriesAllNaNs:                        cfg.Carbon.RenderSeriesAllNaNs,
			CompileEscapeAllNotOnlyQuotes:              cfg.Carbon.CompileEscapeAllNotOnlyQuotes,
			FindResultsIncludeBothExpandableAndLeaf:    cfg.Carbon.FindResultsIncludeBothExpandableAndLeaf,
		}
		if limits := cfg.Carbon.LimitsFind; limits != nil {
			fetchOptsBuilderLimitsOpts := limits.PerQuery.AsFetchOptionsBuilderLimitsOptions()
			graphiteFindFetchOptsBuilder, err = handleroptions.NewFetchOptionsBuilder(
				handleroptions.FetchOptionsBuilderOptions{
					Limits:        fetchOptsBuilderLimitsOpts,
					RestrictByTag: storageRestrictByTags,
					Timeout:       timeout,
				})
			if err != nil {
				logger.Fatal("could not set graphite find fetch options parser", zap.Error(err))
			}
		}
		if limits := cfg.Carbon.LimitsRender; limits != nil {
			fetchOptsBuilderLimitsOpts := limits.PerQuery.AsFetchOptionsBuilderLimitsOptions()
			graphiteRenderFetchOptsBuilder, err = handleroptions.NewFetchOptionsBuilder(
				handleroptions.FetchOptionsBuilderOptions{
					Limits:        fetchOptsBuilderLimitsOpts,
					RestrictByTag: storageRestrictByTags,
					Timeout:       timeout,
				})
			if err != nil {
				logger.Fatal("could not set graphite find fetch options parser", zap.Error(err))
			}
		}
	}

	defaultPrometheusEngine, err := newPromQLEngine(lookbackDuration, cfg, prometheusEngineRegistry, instrumentOptions)
	if err != nil {
		logger.Fatal("unable to create PromQL engine", zap.Error(err))
	}
	prometheusEngineFn := func(lookbackDuration time.Duration) (*prometheuspromql.Engine, error) {
		// NB: use nil metric registry to avoid duplicate metric registration when creating multiple engines
		return newPromQLEngine(lookbackDuration, cfg, nil, instrumentOptions)
	}

	enginesByLookback, err := createEnginesWithResolutionBasedLookbacks(
		lookbackDuration,
		defaultPrometheusEngine,
		runOpts.Config.Clusters,
		cfg.Middleware.Prometheus.ResolutionMultiplier,
		prometheusEngineFn,
	)
	if err != nil {
		logger.Fatal("failed creating PromgQL engines with resolution based lookback durations", zap.Error(err))
	}
	engineCache := promqlengine.NewCache(enginesByLookback, prometheusEngineFn)

	handlerOptions, err := options.NewHandlerOptions(downsamplerAndWriter,
		tagOptions, engine, engineCache.Get, m3dbClusters, clusterClient, cfg,
		runOpts.DBConfig, fetchOptsBuilder, graphiteFindFetchOptsBuilder, graphiteRenderFetchOptsBuilder,
		queryCtxOpts, instrumentOptions, cpuProfileDuration,
		serviceOptionDefaults, httpd.NewQueryRouter(), httpd.NewQueryRouter(),
		graphiteStorageOpts, tsdbOpts, httpd.NewGraphiteRenderRouter(), httpd.NewGraphiteFindRouter(), lookbackDuration)
	if err != nil {
		logger.Fatal("unable to set up handler options", zap.Error(err))
	}

	var customHandlerOpts options.CustomHandlerOptions
	if runOpts.CustomHandlerOptions != nil {
		customHandlerOpts, err = runOpts.CustomHandlerOptions(instrumentOptions)
		if err != nil {
			logger.Fatal("could not create custom handlers", zap.Error(err))
		}
	}

	if fn := customHandlerOpts.OptionTransformFn; fn != nil {
		handlerOptions = fn(handlerOptions)
	}

	customHandlers := customHandlerOpts.CustomHandlers
	handler := httpd.NewHandler(handlerOptions, cfg.Middleware, customHandlers...)
	if err := handler.RegisterRoutes(); err != nil {
		logger.Fatal("unable to register routes", zap.Error(err))
	}

	listenAddress := cfg.ListenAddressOrDefault()
	srvHandler := handler.Router()
	if cfg.HTTP.EnableH2C {
		srvHandler = h2c.NewHandler(handler.Router(), &http2.Server{})
	}
	srv := &http.Server{Addr: listenAddress, Handler: srvHandler}
	defer func() {
		logger.Info("closing server")
		if err := srv.Shutdown(context.Background()); err != nil {
			logger.Error("error closing server", zap.Error(err))
		}
	}()

	listener, err := listenerOpts.Listen("tcp", listenAddress)
	if err != nil {
		logger.Fatal("unable to listen on listen address",
			zap.String("address", listenAddress),
			zap.Error(err))
	}
	if runOpts.ListenerCh != nil {
		runOpts.ListenerCh <- listener
	}
	go func() {
		logger.Info("starting API server", zap.Stringer("address", listener.Addr()))
		if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
			logger.Fatal("server serve error",
				zap.String("address", listenAddress),
				zap.Error(err))
		}
	}()

	if cfg.Ingest != nil {
		logger.Info("starting m3msg server",
			zap.String("address", cfg.Ingest.M3Msg.Server.ListenAddress))
		ingester, err := cfg.Ingest.Ingester.NewIngester(backendStorage,
			tagOptions, instrumentOptions)
		if err != nil {
			logger.Fatal("unable to create ingester", zap.Error(err))
		}

		server, err := cfg.Ingest.M3Msg.NewServer(
			ingester.Ingest, rwOpts,
			instrumentOptions.SetMetricsScope(scope.SubScope("ingest-m3msg")))
		if err != nil {
			logger.Fatal("unable to create m3msg server", zap.Error(err))
		}

		listener, err := listenerOpts.Listen("tcp", cfg.Ingest.M3Msg.Server.ListenAddress)
		if err != nil {
			logger.Fatal("unable to open m3msg server", zap.Error(err))
		}

		if runOpts.M3MsgListenerCh != nil {
			runOpts.M3MsgListenerCh <- listener
		}

		if err := server.Serve(listener); err != nil {
			logger.Fatal("unable to listen on ingest server", zap.Error(err))
		}

		logger.Info("started m3msg server", zap.Stringer("addr", listener.Addr()))
		defer server.Close()
	} else {
		logger.Info("no m3msg server configured")
	}

	if cfg.Carbon != nil && cfg.Carbon.Ingester != nil {
		server := startCarbonIngestion(*cfg.Carbon.Ingester, listenerOpts,
			instrumentOptions, logger, m3dbClusters, clusterNamespacesWatcher,
			downsamplerAndWriter)
		defer server.Close()
	}

	// Stop our async watch and now block waiting for the interrupt.
	intWatchCancel()
	select {
	case <-interruptOpts.InterruptedCh:
		logger.Warn("interrupt already received. closing")
	default:
		xos.WaitForInterrupt(logger, interruptOpts)
	}

	return runResult
}

// make connections to the m3db cluster(s) and generate sessions for those clusters along with the storage
func newM3DBStorage(cfg config.Configuration, clusters m3.Clusters, poolWrapper *pools.PoolWrapper,
	queryContextOptions models.QueryContextOptions, tsdbOpts m3.Options,
	instrumentOptions instrument.Options,
) (storage.Storage, cleanupFn, error) {
	fanoutStorage, storageCleanup, err := newStorages(clusters, cfg,
		poolWrapper, queryContextOptions, tsdbOpts, instrumentOptions)
	if err != nil {
		return nil, nil, errors.Wrap(err, "unable to set up storages")
	}
	logger := instrumentOptions.Logger()
	cleanup := func() error {
		lastErr := storageCleanup()
		// Don't want to quit on the first error since the full cleanup is important
		if lastErr != nil {
			logger.Error("error during storage cleanup", zap.Error(lastErr))
		}

		if err := clusters.Close(); err != nil {
			lastErr = errors.Wrap(err, "unable to close M3DB cluster sessions")
			// Make sure the previous error is at least logged
			logger.Error("error during cluster cleanup", zap.Error(err))
		}

		return lastErr
	}

	return fanoutStorage, cleanup, err
}

func resolveEtcdForM3DB(cfg config.Configuration) (*etcdclient.Configuration, error) {
	etcdConfig := cfg.ClusterManagement.Etcd
	if etcdConfig == nil && len(cfg.Clusters) == 1 &&
		cfg.Clusters[0].Client.EnvironmentConfig != nil {
		syncCfg, err := cfg.Clusters[0].Client.EnvironmentConfig.Services.SyncCluster()
		if err != nil {
			return nil, errors.Wrap(err, "unable to get etcd sync cluster config")
		}
		etcdConfig = syncCfg.Service
	}
	return etcdConfig, nil
}

func newDownsamplerAsync(
	cfg downsample.Configuration, etcdCfg *etcdclient.Configuration, storage storage.Appender,
	clusterNamespacesWatcher m3.ClusterNamespacesWatcher, tagOptions models.TagOptions, clockOpts clock.Options,
	instrumentOptions instrument.Options, rwOpts xio.Options, runOpts RunOptions, interruptOpts xos.InterruptOptions,
) (downsample.Downsampler, clusterclient.Client, error) {
	var (
		clusterClient       clusterclient.Client
		clusterClientWaitCh <-chan struct{}
		err                 error
	)
	if clusterClientCh := runOpts.ClusterClient; clusterClientCh != nil {
		// Only use a cluster client if we are going to receive one, that
		// way passing nil to httpd NewHandler disables the endpoints entirely
		clusterClientDoneCh := make(chan struct{}, 1)
		clusterClientWaitCh = clusterClientDoneCh
		clusterClient = m3dbcluster.NewAsyncClient(func() (clusterclient.Client, error) {
			return <-clusterClientCh, nil
		}, clusterClientDoneCh)
	} else if etcdCfg != nil {
		// We resolved an etcd configuration for cluster management endpoints
		var (
			clusterSvcClientOpts = etcdCfg.NewOptions()
			err                  error
		)
		clusterClient, err = etcdclient.NewConfigServiceClient(clusterSvcClientOpts)
		if err != nil {
			return nil, nil, errors.Wrap(err, "unable to create cluster management etcd client")
		}
	} else if cfg.RemoteAggregator == nil {
		// NB(antanas): M3 Coordinator with in process aggregator can run with in memory cluster client.
		instrumentOptions.Logger().Info("no etcd config and no remote aggregator - will run with in memory cluster client")
		clusterClient = memcluster.New(kv.NewOverrideOptions())
	} else {
		return nil, nil, fmt.Errorf("no configured cluster management config, must set this config for remote aggregator")
	}

	newDownsamplerFn := func() (downsample.Downsampler, error) {
		ds, err := newDownsampler(
			cfg, clusterClient,
			storage, clusterNamespacesWatcher,
			tagOptions, clockOpts, instrumentOptions, rwOpts, runOpts.ApplyCustomRuleStore,
			interruptOpts.InterruptedCh)
		if err != nil {
			return nil, err
		}

		// Notify the downsampler ready channel that
		// the downsampler has now been created and is ready.
		if runOpts.DownsamplerReadyCh != nil {
			runOpts.DownsamplerReadyCh <- struct{}{}
		}

		return ds, nil
	}

	if clusterClientWaitCh != nil {
		// Need to wait before constructing and instead return an async downsampler
		// since the cluster client will return errors until it's initialized itself
		// and will fail constructing the downsampler consequently
		downsampler := downsample.NewAsyncDownsampler(func() (downsample.Downsampler, error) {
			<-clusterClientWaitCh
			return newDownsamplerFn()
		}, nil)
		return downsampler, clusterClient, err
	} else {
		// Otherwise we already have a client and can immediately construct the downsampler
		downsampler, err := newDownsamplerFn()
		return downsampler, clusterClient, err
	}
}

func newDownsampler(
	cfg downsample.Configuration,
	clusterClient clusterclient.Client,
	storage storage.Appender,
	clusterNamespacesWatcher m3.ClusterNamespacesWatcher,
	tagOptions models.TagOptions,
	clockOpts clock.Options,
	instrumentOpts instrument.Options,
	rwOpts xio.Options,
	applyCustomRuleStore downsample.CustomRuleStoreFn,
	interruptedCh <-chan struct{},
) (downsample.Downsampler, error) {
	// Namespace the downsampler metrics.
	instrumentOpts = instrumentOpts.SetMetricsScope(
		instrumentOpts.MetricsScope().SubScope("downsampler"))

	var kvStore kv.Store
	var err error

	if applyCustomRuleStore == nil {
		kvStore, err = clusterClient.KV()
		if err != nil {
			return nil, errors.Wrap(err, "unable to create KV store from the "+
				"cluster management config client")
		}
	} else {
		kvStore, err = applyCustomRuleStore(clusterClient, instrumentOpts)
		if err != nil {
			return nil, errors.Wrap(err, "unable to apply custom rule store")
		}
	}

	tagEncoderOptions := serialize.NewTagEncoderOptions()
	tagDecoderOptions := serialize.NewTagDecoderOptions(serialize.TagDecoderOptionsConfig{})
	tagEncoderPoolOptions := pool.NewObjectPoolOptions().
		SetInstrumentOptions(instrumentOpts.
			SetMetricsScope(instrumentOpts.MetricsScope().
				SubScope("tag-encoder-pool")))
	tagDecoderPoolOptions := pool.NewObjectPoolOptions().
		SetInstrumentOptions(instrumentOpts.
			SetMetricsScope(instrumentOpts.MetricsScope().
				SubScope("tag-decoder-pool")))
	metricsAppenderPoolOptions := pool.NewObjectPoolOptions().
		SetInstrumentOptions(instrumentOpts.
			SetMetricsScope(instrumentOpts.MetricsScope().
				SubScope("metrics-appender-pool")))

	downsampler, err := cfg.NewDownsampler(downsample.DownsamplerOptions{
		Storage:                    storage,
		ClusterClient:              clusterClient,
		RulesKVStore:               kvStore,
		ClusterNamespacesWatcher:   clusterNamespacesWatcher,
		ClockOptions:               clockOpts,
		InstrumentOptions:          instrumentOpts,
		TagEncoderOptions:          tagEncoderOptions,
		TagDecoderOptions:          tagDecoderOptions,
		TagEncoderPoolOptions:      tagEncoderPoolOptions,
		TagDecoderPoolOptions:      tagDecoderPoolOptions,
		TagOptions:                 tagOptions,
		MetricsAppenderPoolOptions: metricsAppenderPoolOptions,
		RWOptions:                  rwOpts,
		InterruptedCh:              interruptedCh,
	})
	if err != nil {
		return nil, fmt.Errorf("unable to create downsampler: %w", err)
	}

	return downsampler, nil
}

func initClusters(
	cfg config.Configuration,
	dbCfg *dbconfig.DBConfiguration,
	clusterNamespacesWatcher m3.ClusterNamespacesWatcher,
	dbClientCh <-chan client.Client,
	encodingOpts encoding.Options,
	localSessionReadyCh chan struct{},
	instrumentOpts instrument.Options,
	customAdminOptions []client.CustomAdminOption,
) (m3.Clusters, *pools.PoolWrapper, error) {
	instrumentOpts = instrumentOpts.
		SetMetricsScope(instrumentOpts.MetricsScope().SubScope("m3db-client"))

	var (
		logger                = instrumentOpts.Logger()
		clusters              m3.Clusters
		poolWrapper           *pools.PoolWrapper
		staticNamespaceConfig bool
		err                   error
	)
	if len(cfg.Clusters) > 0 {
		for _, clusterCfg := range cfg.Clusters {
			if len(clusterCfg.Namespaces) > 0 {
				staticNamespaceConfig = true
				break
			}
		}
		opts := m3.ClustersStaticConfigurationOptions{
			AsyncSessions:      true,
			CustomAdminOptions: customAdminOptions,
			EncodingOptions:    encodingOpts,
		}
		if staticNamespaceConfig {
			clusters, err = cfg.Clusters.NewStaticClusters(instrumentOpts, opts, clusterNamespacesWatcher)
		} else {
			// No namespaces defined in config -- pull namespace data from etcd.
			clusters, err = cfg.Clusters.NewDynamicClusters(instrumentOpts, opts, clusterNamespacesWatcher)
		}
		if err != nil {
			return nil, nil, errors.Wrap(err, "unable to connect to clusters")
		}

		poolWrapper = pools.NewPoolsWrapper(
			pools.BuildIteratorPools(encodingOpts, pools.BuildIteratorPoolsOptions{}))
	} else {
		localCfg := cfg.Local
		if localCfg == nil {
			localCfg = &config.LocalConfiguration{}
		}
		if len(localCfg.Namespaces) > 0 {
			staticNamespaceConfig = true
		}

		if dbClientCh == nil {
			return nil, nil, errors.New("no clusters configured and not running local cluster")
		}

		if localSessionReadyCh == nil {
			localSessionReadyCh = make(chan struct{})
		}
		session := m3db.NewAsyncSession(func() (client.Client, error) {
			return <-dbClientCh, nil
		}, localSessionReadyCh)

		clusterStaticConfig := m3.ClusterStaticConfiguration{
			Namespaces: localCfg.Namespaces,
		}
		if !staticNamespaceConfig {
			if dbCfg == nil {
				return nil, nil, errors.New("environment config required when dynamically fetching namespaces")
			}

			hostID, err := dbCfg.HostIDOrDefault().Resolve()
			if err != nil {
				logger.Fatal("could not resolve hostID",
					zap.Error(err))
			}

			discoveryCfg := dbCfg.DiscoveryOrDefault()
			envCfg, err := discoveryCfg.EnvironmentConfig(hostID)
			if err != nil {
				logger.Fatal("could not get env config from discovery config",
					zap.Error(err))
			}

			clusterStaticConfig.Client = client.Configuration{EnvironmentConfig: &envCfg}
		}

		clustersCfg := m3.ClustersStaticConfiguration{clusterStaticConfig}
		cfgOptions := m3.ClustersStaticConfigurationOptions{
			ProvidedSession:    session,
			CustomAdminOptions: customAdminOptions,
			EncodingOptions:    encodingOpts,
		}

		if staticNamespaceConfig {
			clusters, err = clustersCfg.NewStaticClusters(instrumentOpts, cfgOptions, clusterNamespacesWatcher)
		} else {
			clusters, err = clustersCfg.NewDynamicClusters(instrumentOpts, cfgOptions, clusterNamespacesWatcher)
		}
		if err != nil {
			return nil, nil, errors.Wrap(err, "unable to connect to clusters")
		}

		poolWrapper = pools.NewAsyncPoolsWrapper()
		go func() {
			<-localSessionReadyCh
			poolWrapper.Init(session.IteratorPools())
		}()
	}

	for _, namespace := range clusters.ClusterNamespaces() {
		logger.Info("resolved cluster namespace",
			zap.String("namespace", namespace.NamespaceID().String()))
	}

	return clusters, poolWrapper, nil
}

func newStorages(
	clusters m3.Clusters,
	cfg config.Configuration,
	poolWrapper *pools.PoolWrapper,
	queryContextOptions models.QueryContextOptions,
	opts m3.Options,
	instrumentOpts instrument.Options,
) (storage.Storage, cleanupFn, error) {
	var (
		logger  = instrumentOpts.Logger()
		cleanup = func() error { return nil }
	)

	localStorage, err := m3.NewStorage(clusters, opts, instrumentOpts)
	if err != nil {
		return nil, nil, err
	}

	stores := []storage.Storage{localStorage}
	remoteEnabled := false
	remoteOpts := config.RemoteOptionsFromConfig(cfg.RPC)
	if remoteOpts.ServeEnabled() {
		logger.Info("rpc serve enabled")
		server, err := startGRPCServer(localStorage, queryContextOptions,
			poolWrapper, remoteOpts, instrumentOpts)
		if err != nil {
			return nil, nil, err
		}

		cleanup = func() error {
			server.GracefulStop()
			return nil
		}
	}

	if remoteOpts.ListenEnabled() {
		remoteStorages, enabled, err := remoteClient(poolWrapper, remoteOpts,
			opts, instrumentOpts)
		if err != nil {
			return nil, nil, err
		}

		if enabled {
			stores = append(stores, remoteStorages...)
			remoteEnabled = enabled
		}
	}

	readFilter := filter.LocalOnly
	writeFilter := filter.LocalOnly
	completeTagsFilter := filter.CompleteTagsLocalOnly
	if remoteEnabled {
		// If remote enabled, allow all for read and complete tags
		// but continue to only send writes locally
		readFilter = filter.AllowAll
		completeTagsFilter = filter.CompleteTagsAllowAll
	}

	switch cfg.Filter.Read {
	case config.FilterLocalOnly:
		readFilter = filter.LocalOnly
	case config.FilterRemoteOnly:
		readFilter = filter.RemoteOnly
	case config.FilterAllowAll:
		readFilter = filter.AllowAll
	case config.FilterAllowNone:
		readFilter = filter.AllowNone
	}

	switch cfg.Filter.Write {
	case config.FilterLocalOnly:
		writeFilter = filter.LocalOnly
	case config.FilterRemoteOnly:
		writeFilter = filter.RemoteOnly
	case config.FilterAllowAll:
		writeFilter = filter.AllowAll
	case config.FilterAllowNone:
		writeFilter = filter.AllowNone
	}

	switch cfg.Filter.CompleteTags {
	case config.FilterLocalOnly:
		completeTagsFilter = filter.CompleteTagsLocalOnly
	case config.FilterRemoteOnly:
		completeTagsFilter = filter.CompleteTagsRemoteOnly
	case config.FilterAllowAll:
		completeTagsFilter = filter.CompleteTagsAllowAll
	case config.FilterAllowNone:
		completeTagsFilter = filter.CompleteTagsAllowNone
	}

	fanoutStorage := fanout.NewStorage(stores, readFilter, writeFilter,
		completeTagsFilter, opts.TagOptions(), opts, instrumentOpts)
	return fanoutStorage, cleanup, nil
}

func remoteZoneStorage(
	zone config.Remote,
	poolWrapper *pools.PoolWrapper,
	opts m3.Options,
	instrumentOpts instrument.Options,
) (storage.Storage, error) {
	if len(zone.Addresses) == 0 {
		// No addresses; skip.
		return nil, nil
	}

	client, err := tsdbremote.NewGRPCClient(zone.Name, zone.Addresses,
		poolWrapper, opts, instrumentOpts)
	if err != nil {
		return nil, err
	}

	remoteOpts := remote.Options{
		Name:          zone.Name,
		ErrorBehavior: zone.ErrorBehavior,
	}

	remoteStorage := remote.NewStorage(client, remoteOpts)
	return remoteStorage, nil
}

func remoteClient(
	poolWrapper *pools.PoolWrapper,
	remoteOpts config.RemoteOptions,
	opts m3.Options,
	instrumentOpts instrument.Options,
) ([]storage.Storage, bool, error) {
	logger := instrumentOpts.Logger()
	remotes := remoteOpts.Remotes()
	remoteStores := make([]storage.Storage, 0, len(remotes))
	for _, zone := range remotes {
		logger.Info(
			"creating RPC client with remotes",
			zap.String("name", zone.Name),
			zap.Strings("addresses", zone.Addresses),
		)

		remote, err := remoteZoneStorage(zone, poolWrapper, opts,
			instrumentOpts)
		if err != nil {
			return nil, false, err
		}

		remoteStores = append(remoteStores, remote)
	}

	return remoteStores, true, nil
}

func startGRPCServer(
	storage m3.Storage,
	queryContextOptions models.QueryContextOptions,
	poolWrapper *pools.PoolWrapper,
	opts config.RemoteOptions,
	instrumentOpts instrument.Options,
) (*grpc.Server, error) {
	logger := instrumentOpts.Logger()

	logger.Info("creating gRPC server")
	server := tsdbremote.NewGRPCServer(storage,
		queryContextOptions, poolWrapper, instrumentOpts)

	if opts.ReflectionEnabled() {
		reflection.Register(server)
	}

	logger.Info("gRPC server reflection configured",
		zap.Bool("enabled", opts.ReflectionEnabled()))

	listener, err := net.Listen("tcp", opts.ServeAddress())
	if err != nil {
		return nil, err
	}
	go func() {
		if err := server.Serve(listener); err != nil {
			logger.Error("error from serving gRPC server", zap.Error(err))
		}
	}()

	return server, nil
}

func startCarbonIngestion(
	ingesterCfg config.CarbonIngesterConfiguration,
	listenerOpts xnet.ListenerOptions,
	iOpts instrument.Options,
	logger *zap.Logger,
	m3dbClusters m3.Clusters,
	clusterNamespacesWatcher m3.ClusterNamespacesWatcher,
	downsamplerAndWriter ingest.DownsamplerAndWriter,
) xserver.Server {
	logger.Info("carbon ingestion enabled, configuring ingester")

	// Setup worker pool.
	var (
		carbonIOpts = iOpts.SetMetricsScope(
			iOpts.MetricsScope().SubScope("ingest-carbon"))
		carbonWorkerPoolOpts xsync.PooledWorkerPoolOptions
		carbonWorkerPoolSize int
	)
	if ingesterCfg.MaxConcurrency > 0 {
		// Use a bounded worker pool if they requested a specific maximum concurrency.
		carbonWorkerPoolOpts = xsync.NewPooledWorkerPoolOptions().
			SetGrowOnDemand(false).
			SetInstrumentOptions(carbonIOpts)
		carbonWorkerPoolSize = ingesterCfg.MaxConcurrency
	} else {
		carbonWorkerPoolOpts = xsync.NewPooledWorkerPoolOptions().
			SetGrowOnDemand(true).
			SetKillWorkerProbability(0.001)
		carbonWorkerPoolSize = defaultCarbonIngesterWorkerPoolSize
	}
	workerPool, err := xsync.NewPooledWorkerPool(carbonWorkerPoolSize, carbonWorkerPoolOpts)
	if err != nil {
		logger.Fatal("unable to create worker pool for carbon ingester", zap.Error(err))
	}
	workerPool.Init()

	if m3dbClusters == nil {
		logger.Fatal("carbon ingestion is only supported when connecting to M3DB clusters directly")
	}

	// Create ingester.
	ingester, err := ingestcarbon.NewIngester(
		downsamplerAndWriter, clusterNamespacesWatcher, ingestcarbon.Options{
			InstrumentOptions: carbonIOpts,
			WorkerPool:        workerPool,
			IngesterConfig:    ingesterCfg,
		})
	if err != nil {
		logger.Fatal("unable to create carbon ingester", zap.Error(err))
	}

	// Start server.
	var (
		serverOpts = xserver.NewOptions().
				SetInstrumentOptions(carbonIOpts).
				SetListenerOptions(listenerOpts)
		carbonListenAddress = ingesterCfg.ListenAddressOrDefault()
		carbonServer        = xserver.NewServer(carbonListenAddress, ingester, serverOpts)
	)
	if strings.TrimSpace(carbonListenAddress) == "" {
		logger.Fatal("no listen address specified for carbon ingester")
	}

	logger.Info("starting carbon ingestion server", zap.String("listenAddress", carbonListenAddress))
	err = carbonServer.ListenAndServe()
	if err != nil {
		logger.Fatal("unable to start carbon ingestion server at listen address",
			zap.String("listenAddress", carbonListenAddress), zap.Error(err))
	}

	logger.Info("started carbon ingestion server", zap.String("listenAddress", carbonListenAddress))

	return carbonServer
}

func newDownsamplerAndWriter(
	storage storage.Storage,
	downsampler downsample.Downsampler,
	workerPoolPolicy xconfig.WorkerPoolPolicy,
	iOpts instrument.Options,
) (ingest.DownsamplerAndWriter, error) {
	// Make sure the downsampler and writer gets its own PooledWorkerPool and that its not shared with any other
	// codepaths because PooledWorkerPools can deadlock if used recursively.
	downAndWriterWorkerPoolOpts, writePoolSize := workerPoolPolicy.Options()
	downAndWriterWorkerPoolOpts = downAndWriterWorkerPoolOpts.SetInstrumentOptions(iOpts.
		SetMetricsScope(iOpts.MetricsScope().SubScope("ingest-writer-worker-pool")))
	downAndWriteWorkerPool, err := xsync.NewPooledWorkerPool(writePoolSize, downAndWriterWorkerPoolOpts)
	if err != nil {
		return nil, err
	}
	downAndWriteWorkerPool.Init()

	return ingest.NewDownsamplerAndWriter(storage, downsampler, downAndWriteWorkerPool, iOpts), nil
}

func newPromQLEngine(
	lookbackDelta time.Duration,
	cfg config.Configuration,
	registry extprom.Registerer,
	instrumentOpts instrument.Options,
) (*prometheuspromql.Engine, error) {
	if lookbackDelta < 0 {
		return nil, errors.New("lookbackDelta cannot be negative")
	}

	instrumentOpts.Logger().Debug("creating new PromQL engine", zap.Duration("lookbackDelta", lookbackDelta))
	var (
		kitLogger = kitlogzap.NewZapSugarLogger(instrumentOpts.Logger(), zapcore.InfoLevel)
		opts      = prometheuspromql.EngineOpts{
			Logger:        log.With(kitLogger, "component", "prometheus_engine"),
			Reg:           registry,
			MaxSamples:    cfg.Query.Prometheus.MaxSamplesPerQueryOrDefault(),
			Timeout:       cfg.Query.TimeoutOrDefault(),
			LookbackDelta: lookbackDelta,
			NoStepSubqueryIntervalFn: func(rangeMillis int64) int64 {
				return durationMilliseconds(1 * time.Minute)
			},
		}
	)
	return prometheuspromql.NewEngine(opts), nil
}

func createEnginesWithResolutionBasedLookbacks(
	defaultLookback time.Duration,
	defaultEngine *prometheuspromql.Engine,
	clusters m3.ClustersStaticConfiguration,
	resolutionMultiplier int,
	prometheusEngineFn func(time.Duration) (*prometheuspromql.Engine, error),
) (map[time.Duration]*prometheuspromql.Engine, error) {
	enginesByLookback := make(map[time.Duration]*prometheuspromql.Engine)
	enginesByLookback[defaultLookback] = defaultEngine
	if resolutionMultiplier > 0 {
		for _, cluster := range clusters {
			for _, ns := range cluster.Namespaces {
				if res := ns.Resolution; res > 0 {
					resolutionBasedLookback := res * time.Duration(resolutionMultiplier)
					if _, ok := enginesByLookback[resolutionBasedLookback]; !ok {
						eng, err := prometheusEngineFn(resolutionBasedLookback)
						if err != nil {
							return nil, err
						}
						enginesByLookback[resolutionBasedLookback] = eng
					}
				}
			}
		}
	}
	return enginesByLookback, nil
}

func durationMilliseconds(d time.Duration) int64 {
	return int64(d / (time.Millisecond / time.Nanosecond))
}


text:
import sys, os, glob, time, datetime, stat, json, tempfile, shutil, psutil, random, urlparse, getpass
import h2o_args
import h2o_nodes
import h2o_sandbox

# print "h2o_test"

# this is just for putting timestamp in front of all stdout
class OutWrapper:
    def __init__(self, out):
        self._out = out

    def write(self, x):
            # got this with random data to parse.. why? it shows up in our stdout?
            # UnicodeEncodeError:
            #  'ascii' codec can't encode character u'\x80' in position 41: ordinal not in range(128)
            # could we be getting unicode object, or is it just the bytes
            try:
                s = x.replace('\n', '\n[{0}] '.format(datetime.datetime.now()))
                self._out.write(s)
            except:
                self._out.write(s.encode('utf8'))

    def flush(self):
        self._out.flush()

def verboseprint(*args, **kwargs):
    if h2o_args.verbose:
        for x in args: # so you don't have to create a single string
            print x,
        for x in kwargs: # so you don't have to create a single string
            print x,
        print
        # so we can see problems when hung?
        sys.stdout.flush()

def sleep(secs):
    if getpass.getuser() == 'jenkins':
        period = max(secs, 120)
    else:
        period = secs
        # if jenkins, don't let it sleep more than 2 minutes
    # due to left over h2o.sleep(3600)
    time.sleep(period)

def find_file(base):
    # epand ~ or ~user with home dir
    f = os.path.expanduser(base)
    if not os.path.exists(f): f = '../' + base
    if not os.path.exists(f): f = '../../' + base
    if not os.path.exists(f): f = '../../../' + base
    if not os.path.exists(f): f = 'py/' + base
    # these 2 are for finding from h2o-perf
    if not os.path.exists(f): f = '../h2o/' + base
    if not os.path.exists(f): f = '../../h2o/' + base
    if not os.path.exists(f):
        raise Exception("unable to find file %s" % base)
    return f

# The cloud is uniquely named per user (only) and pid
# do the flatfile the same way
# Both are the user that runs the test. The config might have a different username on the
# remote machine (0xdiag, say, or hduser)
def flatfile_pathname():
    return (LOG_DIR + '/pytest_flatfile-%s' % getpass.getuser())

# used to rename the sandbox when running multiple tests in same dir (in different shells)
def get_sandbox_name():
    if os.environ.has_key("H2O_SANDBOX_NAME"):
        a = os.environ["H2O_SANDBOX_NAME"]
        print "H2O_SANDBOX_NAME", a
        return a
    else:
        return "sandbox"

# shutil.rmtree doesn't work on windows if the files are read only.
# On unix the parent dir has to not be readonly too.
# May still be issues with owner being different, like if 'system' is the guy running?
# Apparently this escape function on errors is the way shutil.rmtree can
# handle the permission issue. (do chmod here)
# But we shouldn't have read-only files. So don't try to handle that case.

def handleRemoveError(func, path, exc):
    # If there was an error, it could be due to windows holding onto files.
    # Wait a bit before retrying. Ignore errors on the retry. Just leave files.
    # Ex. if we're in the looping cloud test deleting sandbox.
    excvalue = exc[1]
    print "Retrying shutil.rmtree of sandbox. Will ignore errors. Exception was", excvalue.errno
    time.sleep(2)
    try:
        func(path)
    except OSError:
        pass

LOG_DIR = get_sandbox_name()

def clean_sandbox():
    IS_THIS_FASTER = True
    if os.path.exists(LOG_DIR):

        # shutil.rmtree hangs if symlinks in the dir? (in syn_datasets for multifile parse)
        # use os.remove() first
        for f in glob.glob(LOG_DIR + '/syn_datasets/*'):
            verboseprint("cleaning", f)
            os.remove(f)

        # shutil.rmtree fails to delete very long filenames on Windoze
        ### shutil.rmtree(LOG_DIR)
        # was this on 3/5/13. This seems reliable on windows+cygwin
        # I guess I changed back to rmtree below with something to retry, then ignore, remove errors.
        # is it okay now on windows+cygwin?
        ### os.system("rm -rf "+LOG_DIR)
        print "Removing", LOG_DIR, "(if slow, might be old ice dir spill files)"
        start = time.time()
        if IS_THIS_FASTER:
            try:
                os.system("rm -rf "+LOG_DIR)
            except OSError:
                pass
        else:
            shutil.rmtree(LOG_DIR, ignore_errors=False, onerror=handleRemoveError)

        elapsed = time.time() - start
        print "Took %s secs to remove %s" % (elapsed, LOG_DIR)
        # it should have been removed, but on error it might still be there

    if not os.path.exists(LOG_DIR):
        os.mkdir(LOG_DIR)

# who knows if this one is ok with windows...doesn't rm dir, just
# the stdout/stderr files
def clean_sandbox_stdout_stderr():
    if os.path.exists(LOG_DIR):
        files = []
        # glob.glob returns an iterator
        for f in glob.glob(LOG_DIR + '/*stdout*'):
            verboseprint("cleaning", f)
            os.remove(f)
        for f in glob.glob(LOG_DIR + '/*stderr*'):
            verboseprint("cleaning", f)
            os.remove(f)

def clean_sandbox_doneToLine():
    if os.path.exists(LOG_DIR):
        files = []
        # glob.glob returns an iterator
        for f in glob.glob(LOG_DIR + '/*doneToLine*'):
            verboseprint("cleaning", f)
            os.remove(f)

# just use a global here for the sticky state
sandbox_error_was_reported = False
def check_sandbox_for_errors(cloudShutdownIsError=False, sandboxIgnoreErrors=False, python_test_name=''):
    # dont' have both tearDown and tearDownClass report the same found error
    # only need the first
    global sandbox_error_was_reported
    if sandbox_error_was_reported: # gets current state
        return

    # Can build a cloud that ignores all sandbox things that normally fatal the test
    # Kludge, test will set this directly if it wants, rather than thru build_cloud parameter.
    # we need the sandbox_ignore_errors, for the test teardown_cloud..the state disappears!
    ignore = sandboxIgnoreErrors or (h2o_nodes.nodes and h2o_nodes.nodes[0].sandbox_ignore_errors)
    errorFound = h2o_sandbox.check_sandbox_for_errors(
        LOG_DIR=LOG_DIR,
        sandboxIgnoreErrors=ignore,
        cloudShutdownIsError=cloudShutdownIsError,
        python_test_name=python_test_name)

    if errorFound:
        sandbox_error_was_reported = True

def tmp_file(prefix='', suffix='', tmp_dir=None):
    if not tmp_dir:
        tmpdir = LOG_DIR
    else:
        tmpdir = tmp_dir

    fd, path = tempfile.mkstemp(prefix=prefix, suffix=suffix, dir=tmpdir)
    # make sure the file now exists
    # os.open(path, 'a').close()
    # give everyone permission to read it (jenkins running as
    # 0xcustomer needs to archive as jenkins
    permissions = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
    os.chmod(path, permissions)
    return (fd, path)

def tmp_dir(prefix='', suffix=''):
    return tempfile.mkdtemp(prefix=prefix, suffix=suffix, dir=LOG_DIR)

def make_syn_dir():
    # move under sandbox
    # the LOG_DIR must have been created for commands.log before any datasets would be created
    SYNDATASETS_DIR = LOG_DIR + '/syn_datasets'
    if os.path.exists(SYNDATASETS_DIR):
        shutil.rmtree(SYNDATASETS_DIR)
    os.mkdir(SYNDATASETS_DIR)
    return SYNDATASETS_DIR

def log_rest(s):
    if not h2o_args.debug_rest:
        return
    rest_log_file = open(os.path.join(LOG_DIR, "rest.log"), "a")
    rest_log_file.write(s)
    rest_log_file.write("\n")
    rest_log_file.close()

def log(cmd, comment=None):
    filename = LOG_DIR + '/commands.log'
    # everyone can read
    with open(filename, 'a') as f:
        f.write(str(datetime.datetime.now()) + ' -- ')
        # what got sent to h2o
        # f.write(cmd)
        # let's try saving the unencoded url instead..human readable
        if cmd:
            f.write(urlparse.unquote(cmd))
            if comment:
                f.write('    #')
                f.write(comment)
            f.write("\n")
        elif comment: # for comment-only
            f.write(comment + "\n")
            # jenkins runs as 0xcustomer,
            # and the file wants to be archived by jenkins who isn't in his group
    permissions = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
    os.chmod(filename, permissions)

def dump_json(j):
    return json.dumps(j, sort_keys=True, indent=2)

# can't have a list of cmds, because cmd is a list
# cmdBefore gets executed first, and we wait for it to complete
def spawn_cmd(name, cmd, capture_output=True, **kwargs):
    if capture_output:
        outfd, outpath = tmp_file(name + '.stdout.', '.log')
        errfd, errpath = tmp_file(name + '.stderr.', '.log')
        # everyone can read
        ps = psutil.Popen(cmd, stdin=None, stdout=outfd, stderr=errfd, **kwargs)
    else:
        outpath = '<stdout>'
        errpath = '<stderr>'
        ps = psutil.Popen(cmd, **kwargs)

    comment = 'PID %d, stdout %s, stderr %s' % (
        ps.pid, os.path.basename(outpath), os.path.basename(errpath))
    log(' '.join(cmd), comment=comment)
    return (ps, outpath, errpath)

def spawn_wait(ps, stdout, stderr, capture_output=True, timeout=None):
    rc = ps.wait(timeout)
    if capture_output:
        out = file(stdout).read()
        err = file(stderr).read()
    else:
        out = 'stdout not captured'
        err = 'stderr not captured'

    if rc is None:
        ps.terminate()
        raise Exception("%s %s timed out after %d\nstdout:\n%s\n\nstderr:\n%s" %
                        (ps.name, ps.cmdline, timeout or 0, out, err))
    elif rc != 0:
        raise Exception("%s %s failed.\nstdout:\n%s\n\nstderr:\n%s" %
                        (ps.name, ps.cmdline, out, err))
    return rc

def spawn_cmd_and_wait(name, cmd, capture_output=True, timeout=None, **kwargs):
    (ps, stdout, stderr) = spawn_cmd(name, cmd, capture_output, **kwargs)
    spawn_wait(ps, stdout, stderr, capture_output, timeout)

def check_h2o_version():
    # assumes you want to know about 3 ports starting at base_port
    command1Split = ['java', '-jar', find_file('target/h2o.jar'), '--version']
    command2Split = ['egrep', '-v', '( Java | started)']
    print "Running h2o to get java version"
    p1 = Popen(command1Split, stdout=PIPE)
    p2 = Popen(command2Split, stdin=p1.stdout, stdout=PIPE)
    output = p2.communicate()[0]
    print output

def setup_random_seed(seed=None):
    if h2o_args.random_seed is not None:
        SEED = h2o_args.random_seed
    elif seed is not None:
        SEED = seed
    else:
        SEED = random.randint(0, sys.maxint)
    random.seed(SEED)
    print "\nUsing random seed:", SEED
    return SEED

redpajama_set_name: RedPajamaGithub


text:
"""
Author: Eric J. Ma
Affiliation: Massachusetts Institute of Technology
"""

from generate_id import generate_id
from copy import copy, deepcopy
from random import choice
from numpy.random import normal

class Pathogen(object):
	"""The Pathogen class models a generic pathogen."""
	def __init__(self, segments, creation_time, progeny_size, \
		parent=dict(), convenient_id=None):
		"""
		Initialize the pathogen.

		ATTRIBUTES:
		-	LIST: segments
				a list of Segment objects that specify the genomic segments
				that are present in the pathogen.

		-	INT: creation_time
				an integer number that specifies the time within the
				simulation that the pathogen was created.

		- 	DICTIONARY: parent
				A dictionary that specifies the parent of the pathogen.
				The (key, value) pairs are (parent, segments transmitted).
					-	If the dictionary is empty, then the pathogen is a seed pathogen.
					-	If the dictionary has one (key, value) pair, then the pathogen was replicated from one other pathogen.
					-	If the dictionary has two (key, value) pairs, then the pathogen was a reassortant of two parental pathogens.

		-	STRING: id
				a string generated form the generate_id function that uniquely
				identifies the pathogen.

		-	TUPLE: progeny_size
				a two-tuple that describes the (mean, var) of the burst size
				per pathogen. These are the Normal distribution parameters. In
				practice, while a floating point number is drawn from the
				Normal distribution, it will be rounded to an integer number.
		"""
		super(Pathogen, self).__init__()

		self.segments = segments

		self.creation_time = creation_time

		self.parent = parent

		self.id = generate_id()

		# self.convenient_id = self.id[0:5]

		self.progeny_size = progeny_size

	def __repr__(self):
		return self.id[0:5]

	def reassort_with(self, other, current_time):
		"""
		This method takes in another pathogen and returns a progeny with
		genomic segments that are randomly selected from segments from both
		pathogens. Reassortment with another pathogen is akin to
		replication, but the step at which segments are added is different.

		INPUTS:
		-	OBJECT: other
				The other pathogen with which to reassort.

		-	INT: current_time
				The time at which the reassortment event happened.

		OUTPUTS:
		-	OBJECT: new_pathogen
				The reassortant progeny from the two pathogens.
		"""

		new_pathogen = copy(self)
		new_pathogen.creation_time = current_time
		new_pathogen.id = generate_id()
		new_pathogen.segments = []

		# Assign parent differently from the replicate() function.
		parent = dict()
		parent[self] = []
		parent[other] = []

		# Iterate over all of the segments.
		for segment in self.segments:
			i = choice([0, 1])
			segment_number = segment.segment_number
			segments_to_choose_from = (segment, \
				other.get_segment(segment_number))
			new_pathogen.segments.append(deepcopy(segments_to_choose_from[i]))
			if i == 0:
				parent[self].append(segments_to_choose_from[0].segment_number)
			if i == 1:
				parent[other].append(segments_to_choose_from[1].segment_number)

		# Clean the dictionary if the progeny is entirely derived from one
		# parent.
		if parent[self] == []:
			del parent[self]
		if parent[other] == []:
			del parent[other]

		new_pathogen.parent = parent

		new_pathogen.mutate()

		# print new_pathogen.parent
		return new_pathogen

	def generate_progeny(self, current_time):
		"""
		This method takes calls on the replicate() function to generate n
		progeny, drawn from the Normal distribution.

		INPUTS:
		-	INT: current_time:
				This number will be set to the "creation_time" of the new
				pathogen, and is passed into the replicate() function.

		OUTPUTS:
		-	LIST: progeny
				A list of progeny pathogens that can be extended onto a
				"master list" of pathogens.
		"""
		mean = self.progeny_size[0]
		var = self.progeny_size[1]
		num_progeny = normal(mean, var)

		progeny = []
		for i in range(num_progeny):
			progeny.append(self.replicate(current_time))

		return progeny

	def replicate(self, current_time): #, convenient_id=None):
		"""
		This method replicates the pathogen once. Mutation is guaranteed to be
		called, but not guaranteed to happen, as it depends on the
		substitution rate of each of the genomic segments.

		INPUTS:
		-	INT: current_time
				This number will be set to the "creation_time" of the new
				pathogen.

		OUTPUTS:
		-	OBJECT: new_pathogen
				The replicated pathogen.
		"""
		new_pathogen = copy(self)
		new_pathogen.parent = dict()
		new_pathogen.parent[self] = self.get_segment_numbers()
		new_pathogen.creation_time = current_time
		new_pathogen.id = generate_id()
		new_pathogen.segments = deepcopy(self.segments)
		new_pathogen.mutate()

		# print new_pathogen.parent
		return new_pathogen

	def mutate(self):
		"""
		This method mutates each of the genomic segments according to
		their specified substitution rates. It does not return anything.
		"""
		for segment in self.segments:
			segment.mutate()

	#################### PATHOGEN PROPERTY METHODS ############################

	def is_seed(self):
		"""
		This method returns a Boolean value telling us if the pathogen is
		a "seed" pathogen. A "seed" pathogen is one that "seeded" the
		population of pathogens present in the population.
		"""
		if len(self.parent) == 0:
			return True
		else:
			return False

	def is_reassorted(self):
		"""
		This method returns a Boolean value telling us if the pathogen is
		a "reassorted" pathogen. A "reassorted" pathogen is one that has
		genomic segments from two different parents.
		"""

		if len(self.parent) == 2:
			return True
		else:
			return False

	def mutations(self):
		"""
		This method prints the mutations present inside the pathogen.
		"""
		for segment in self.segments:
			print segment.mutations

	def get_segment(self, segment_number):
		"""
		This method returns the given segment by segment number.

		INPUTS:
		-	INT: segment_number
				The integer number of the segment corresponding to the Segment
				object's segment_number attribute

		OUTPUTS:
		-	SEGMENT OBJECT: seg
				The Segment object from the pathogen that has the
				corresponding Segment number as specified in its
				segment_number attribute.
		"""
		for segment in self.segments:
			if segment.segment_number == segment_number:
				return segment

	def get_segment_numbers(self):
		"""
		This method returns a list of all of the segment numbers for each of
		the segments present in the pathogen.
		"""
		segment_numbers = []
		for segment in self.segments:
			segment_numbers.append(segment.segment_number)

		return segment_numbers


text:
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Copyright:
 *     Christian Schulte, 2009
 *
 *  Last modified:
 *     $Date: 2010-03-03 16:38:41 +0100 (on, 03 mar 2010) $
 *     $Revision: 10359 $
 *
 *  This file is part of Gecode, the generic constraint
 *  development environment:
 *     http://www.gecode.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

namespace Gecode { namespace Int { namespace Sequence {

  /// Status of whether a view takes a value from a set
  enum TakesStatus {
    TS_NO,   ///< Definitely not
    TS_YES,  ///< Definitely yes
    TS_MAYBE ///< Maybe or maybe not
  };

  /// Return whether view \a x takes value \a s
  template<class View>
  forceinline TakesStatus
  takes(const View& x, int s) {
    if (x.in(s))
      return x.assigned() ? TS_YES : TS_MAYBE;
    else
      return TS_NO;
  }
  /// Return whether view \a x takes value from \a s
  template<class View>
  forceinline TakesStatus
  takes(const View& x, const IntSet& s) {
    if ((x.max() < s.min()) || (x.min() > s.max()))
      return TS_NO;
    ViewRanges<View> ix(x);
    IntSetRanges is(s);
    switch (Iter::Ranges::compare(ix,is)) {
    case Iter::Ranges::CS_SUBSET: return TS_YES;
    case Iter::Ranges::CS_DISJOINT: return TS_NO;
    case Iter::Ranges::CS_NONE: return TS_MAYBE;
    default: GECODE_NEVER;
    }
    return TS_MAYBE;
  }

  /// Test whether all values of view \a x are included in \a s
  template<class View>
  forceinline bool
  includes(const View& x, int s) {
    return x.assigned() && x.in(s);
  }
  /// Test whether all values of view \a x are included in \a s
  template<class View>
  forceinline bool
  includes(const View& x, const IntSet& s) {
    if ((x.max() < s.min()) || (x.min() > s.max()))
      return false;
    ViewRanges<View> ix(x);
    IntSetRanges is(s);
    return Iter::Ranges::subset(ix,is);
  }

  /// Test whether all values of view \a x are excluded from \a s
  template<class View>
  forceinline bool
  excludes(const View& x, int s) {
    return !x.in(s);
  }
  /// Test whether all values of view \a x are excluded from \a s
  template<class View>
  forceinline bool
  excludes(const View& x, const IntSet& s) {
    if ((x.max() < s.min()) || (x.min() > s.max()))
      return true;
    ViewRanges<View> ix(x);
    IntSetRanges is(s);
    return Iter::Ranges::disjoint(ix,is);
  }

  /// Test whether no decision on inclusion or exclusion of values of view \a x in \a s can be made
  template<class View>
  forceinline bool
  undecided(const View& x, int s) {
    return !x.assigned() && x.in(s);
  }
  /// Test whether no decision on inclusion or exclusion of values of view \a x in \a s can be made
  template<class View>
  forceinline bool
  undecided(const View& x, const IntSet& s) {
    if ((x.max() < s.min()) || (x.min() > s.max()))
      return false;
    ViewRanges<View> ix(x);
    IntSetRanges is(s);
    return Iter::Ranges::compare(ix,is) == Iter::Ranges::CS_NONE;
  }

  /// Prune view \a x to only include values from \a s
  template<class View>
  forceinline ModEvent
  include(Space& home, View& x, int s) {
    return x.eq(home,s);
  }
  /// Prune view \a x to only include values from \a s
  template<class View>
  forceinline ModEvent
  include(Space& home, View& x, const IntSet& s) {
    IntSetRanges is(s);
    return x.inter_r(home,is,false);
  }

  /// Prune view \a x to exclude all values from \a s
  template<class View>
  forceinline ModEvent
  exclude(Space& home, View& x, int s) {
    return x.nq(home,s);
  }
  /// Prune view \a x to exclude all values from \a s
  template<class View>
  forceinline ModEvent
  exclude(Space& home, View& x, const IntSet& s) {
    IntSetRanges is(s);
    return x.minus_r(home,is,false);
  }

}}}

// STATISTICS: int-prop


text:
package org.hisp.dhis.user;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Can be used to annotate controller parameters of type {@link User}, if the
 * current user as returned by the {@link CurrentUserService} should be
 * injected.
 *
 * If the annotation is used with a {@link String} parameter the
 * {@link CurrentUserService#getCurrentUsername()} is injected.
 *
 * @author Jan Bernitt
 */
@Target( ElementType.PARAMETER )
@Retention( RetentionPolicy.RUNTIME )
public @interface CurrentUser
{
    /**
     * @return When true, the current user must be present otherwise an
     *         exception is thrown when resolving the parameter.
     */
    boolean required() default false;
}


text:
require File.dirname(__FILE__) + '/../../spec_helper'

describe Admin::CategoriesController do
  integrate_views

  before do
    request.session = { :user => users(:tobi).id }
  end

  def test_index
    get :index
    assert_template 'index'
    assert_template_has 'categories'
    assert_tag :tag => "div",
      :attributes => { :id => "category_container" }
  end

  def test_create
    assert_difference 'Category.count' do
      post :edit, 'category' => { :name => "test category" }
      assert_response :redirect, :action => 'index'
    end
  end

  def test_edit
    get :edit, :id => categories(:software).id
    assert_template 'new'
    assert_template_has 'category'
    assert assigns(:category).valid?
  end

  def test_update
    post :edit, :id => categories(:software).id
    assert_response :redirect, :action => 'index'
  end

  def test_destroy
    test_id = categories(:software).id
    assert_not_nil Category.find(test_id)

    get :destroy, :id => test_id
    assert_response :success
    assert_template 'destroy'

    post :destroy, :id => test_id
    assert_response :redirect, :action => 'index'

    assert_raise(ActiveRecord::RecordNotFound) { Category.find(test_id) }
  end

  def test_order
    assert_equal categories(:software), Category.find(:first, :order => :position)
    get :order, :category_list => [categories(:personal).id, categories(:hardware).id, categories(:software).id]
    assert_response :success
    assert_equal categories(:personal), Category.find(:first, :order => :position)
  end

  def test_asort
    assert_equal categories(:software), Category.find(:first, :order => :position)
    get :asort
    assert_response :success
    assert_template "_categories"
    assert_equal categories(:hardware), Category.find(:first, :order => :position)
  end

  def test_category_container
    get :category_container
    assert_response :success
    assert_template "_categories"
    assert_tag :tag => "table",
      :children => { :count => Category.count + 2,
        :only => { :tag => "tr",
          :children => { :count => 2,
            :only => { :tag => /t[dh]/ } } } }
  end

  def test_reorder
    get :reorder
    assert_response :success
    assert_template "reorder"
    assert_select 'ul#category_list > li', Category.count
    assert_select 'a', '(Done)'
  end
end


text:
import {
  Uniform
} from '../../../src/Three';

export interface ColorifyShader {
  uniforms: {
    tDiffuse: Uniform;
    color: Uniform;
  };
  vertexShader: string;
  fragmentShader: string;
}


text:
package rsync

import (
	"fmt"
	"os"
	"testing"
	"time"

	"github.com/chrislusf/glow/util"
)

func TestNormalHttpCopy(t *testing.T) {

	util.SetupHttpClient(nil)

	files := []string{
		os.Args[0], "http_server.go", "../scheduler/scheduler.go", "fetch_url.go",
	}

	rsyncServer, err := NewRsyncServer(files[0], files[1:])
	if err != nil {
		t.Fatalf("Failed to start local server: %v", err)
	}
	rsyncServer.StartRsyncServer(nil, ":0")

	err = FetchFilesTo(fmt.Sprintf("localhost:%d", rsyncServer.Port), "/tmp")
	if err != nil {
		fmt.Printf("pausing localhost:%d\n", rsyncServer.Port)
		time.Sleep(time.Minute)
		t.Fatalf("Failed to download file: %v", err)
	}

	t.Logf("file downloading works ok.")
}


text: module Module(module Unimported) where


text:
package ch.carve.consul;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;

import ch.carve.consul.discovery.DiscoverableService;
import ch.carve.consul.discovery.ServiceProvider;

@Path("/discovery")
public class DiscoveryResource {

    private static final String SERVICE_NAME = "hello";
    private static final String PATH = "/hello/v1/hello";

    private static Client client = new ResteasyClientBuilder()
            .maxPooledPerRoute(20)
            .connectionPoolSize(60)
            .socketTimeout(10, TimeUnit.SECONDS)
            .build();

    @Inject
    @DiscoverableService(serviceName = SERVICE_NAME)
    private ServiceProvider service;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() throws Exception {
        return client.target(service.createUri(PATH)).request().get(String.class);
    }
}


text:
const appbase = require('..');
const { uuidv4 } = require('../__mocks__');
const config = require('../__mocks__/defaultConfig');

describe('#Update', function() {
  let client;
  beforeAll(() => {
    client = appbase({
      url: config.URL,
      app: config.APPNAME,
      credentials: config.CREDENTIALS,
    });
  });

  test('should update one document', async () => {
    const id = uuidv4();
    var tweet = {
      user: 'olivere',
      message: 'Welcome to Golang and Elasticsearch.',
    };
    var tweet2 = {
      user: 'olivere',
      message: 'This is a new tweet.',
    };
    await client.index({
      id,
      body: tweet,
    });
    const updateRes = await client.update({
      id,
      body: {
        doc: tweet2
      },
    })
    expect(updateRes.result).toEqual('updated');
  });
});


text:
/*global _, Backbone*/
//We get the model of the collection.
var Notification = require('./notification');
//This collection will contains all the message which will be display in the application.
module.exports = Backbone.Collection.extend({
	model: Notification,
	// Return a constructed object filtered by error type.
	getMessagesByType: function filterByTypeMessage() {
		var jsonCollection = this.toJSON();
		var messages = {};
		messages.errorMessages = _.where(jsonCollection, {
			'type': 'error'
		});
		messages.warningMessages = _.where(jsonCollection, {
			'type': 'warning'
		});
		messages.infoMessages = _.where(jsonCollection, {
			'type': 'info'
		});
		messages.successMessages = _.where(jsonCollection, {
			'type': 'success'
		});
		return messages;
	},
	//Create a comparatot method in order to be able to sort them on their type
	comparator: function comparator(notification) {
		return notification.get("creationDate");
	}
});


text:
{% extends "org_template.html" %}
{% from "components/page-header.html" import page_header %}
{% from "components/page-footer.html" import page_footer %}
{% from "components/form.html" import form_wrapper %}
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}

{% block org_page_title %}
  Change organisation name
{% endblock %}

{% block backLink %}
  {{ govukBackLink({ "href": url_for('.organisation_settings', org_id=current_org.id) }) }}
{% endblock %}

{% block maincolumn_content %}

  {{ page_header('Change organisation name') }}

  {% call form_wrapper() %}
    {{ form.name }}
    {{ page_footer('Save') }}
  {% endcall %}

{% endblock %}


text:
package scutil.lang

import minitest.*

object DisposerTest extends SimpleTestSuite {
	test("a combined Disposer should execute both actions in order") {
		var tmp	= ""
		val a	= Disposer delay { tmp = tmp + "a" }
		val b	= Disposer delay { tmp = tmp + "b" }
		val c	= a combine b
		c.dispose()

		assertEquals(tmp, "ab")
	}

	test("in an exception thrown in the first of a combined Disposer should be work") {
		var tmp	= 0
		var err	= null:Exception
		val a	= Disposer delay { sys error "a failed" }
		val b	= Disposer delay { tmp = 2 }
		val c	= a combine b
		try {
			c.dispose()
		}
		catch { case e:Exception =>
			err = e
		}
		assertEquals(tmp, 2)
		assertEquals(err.getMessage, "a failed")
	}

	test("in an exception thrown in the second of a combined Disposer should be work") {
		var tmp	= 0
		var err	= null:Exception
		val a	= Disposer delay { tmp = 1 }
		val b	= Disposer delay { sys error "b failed" }
		val c	= a combine b
		try {
			c.dispose()
		}
		catch { case e:Exception =>
			err = e
		}
		assertEquals(tmp, 1)
		assertEquals(err.getMessage, "b failed")
	}

	test("in an exception thrown in both a combined Disposer should be work") {
		var err	= null:Exception
		val a	= Disposer delay { sys error "a failed" }
		val b	= Disposer delay { sys error "b failed" }
		val c	= a combine b
		try {
			c.dispose()
		}
		catch { case e:Exception =>
			err = e
		}
		assertEquals(err.getMessage, "a failed")
		assertEquals(err.getSuppressed.length, 1)
		assertEquals(err.getSuppressed()(0).getMessage, "b failed")
	}
}


text:
import unittest

import sys
sys.path.append('LeetCode/_051_100')
sys.path.append('LeetCode.Test')

from _068_TextJustification import Solution
import AssertHelper

class Test_068_TextJustification(unittest.TestCase):
    def test_fullJustify_1(self):
        solution = Solution()
        result = solution.fullJustify(["This", "is", "an", "example", "of", "text", "justification."], 16)
        AssertHelper.assertArray([
            "This    is    an",
            "example  of text",
            "justification.  "
        ], result)

    def test_fullJustify_2(self):
        solution = Solution()
        result = solution.fullJustify(["What","must","be","acknowledgment","shall","be"], 16)
        AssertHelper.assertArray([
            "What   must   be",
            "acknowledgment  ",
            "shall be        "
        ], result)

    def test_fullJustify_3(self):
        solution = Solution()
        result = solution.fullJustify(["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], 20)
        AssertHelper.assertArray([
            "Science  is  what we",
            "understand      well",
            "enough to explain to",
            "a  computer.  Art is",
            "everything  else  we",
            "do                  "
        ], result)


text:
package org.linguafranca.pwdb.kdbx.simple;

import org.linguafranca.pwdb.base.AbstractDatabase;
import org.linguafranca.pwdb.kdbx.Helpers;
import org.linguafranca.pwdb.kdbx.StreamEncryptor;
import org.linguafranca.pwdb.kdbx.simple.transformer.KdbxInputTransformer;
import org.linguafranca.pwdb.kdbx.simple.transformer.KdbxOutputTransformer;
import org.linguafranca.pwdb.kdbx.KdbxHeader;
import org.linguafranca.pwdb.kdbx.KdbxSerializer;
import org.linguafranca.pwdb.kdbx.simple.converter.*;
import org.linguafranca.pwdb.kdbx.simple.model.EntryClasses;
import org.linguafranca.pwdb.kdbx.simple.model.KeePassFile;
import org.linguafranca.pwdb.Credentials;
import org.linguafranca.xml.XmlInputStreamFilter;
import org.linguafranca.xml.XmlOutputStreamFilter;
import org.simpleframework.xml.*;
import org.simpleframework.xml.convert.AnnotationStrategy;
import org.simpleframework.xml.convert.Registry;
import org.simpleframework.xml.convert.RegistryStrategy;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.strategy.Strategy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

/**
 * Implementation of {@link org.linguafranca.pwdb.Database} using the Simple XML framework.
 *
 * @author jo
 */
@SuppressWarnings("WeakerAccess")
public class SimpleDatabase extends AbstractDatabase<SimpleDatabase, SimpleGroup, SimpleEntry, SimpleIcon>{

    KeePassFile keePassFile;

    /**
     * Create a new empty database
     */
    public SimpleDatabase() {
        try {
            keePassFile = createEmptyDatabase();
            keePassFile.root.group.database = this;
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Create a database instance from a keepass file
     * @param keePassFile the instance to initialise from
     */
    protected SimpleDatabase (KeePassFile keePassFile) {
        this.keePassFile = keePassFile;
        this.keePassFile.root.group.database = this;
        fixUp(this.keePassFile.root.group);
    }

    @Override
    public SimpleGroup getRootGroup() {
        return keePassFile.root.getGroup();
    }

    @Override
    public SimpleGroup newGroup() {
        return SimpleGroup.createGroup(this);
    }

    @Override
    public SimpleEntry newEntry() {
        return SimpleEntry.createEntry(this);
    }

    @Override
    public SimpleIcon newIcon() {
        return new SimpleIcon();
    }

    @Override
    public SimpleIcon newIcon(Integer integer) {
        SimpleIcon ic = newIcon();
        ic.setIndex(integer);
        return ic;
    }

    @Override
    public boolean isRecycleBinEnabled() {
        return this.keePassFile.meta.recycleBinEnabled;
    }

    @Override
    public void enableRecycleBin(boolean enable) {
        this.keePassFile.meta.recycleBinEnabled = enable;
    }

    @Override
    public SimpleGroup getRecycleBin() {
        UUID recycleBinUuid = this.keePassFile.meta.recycleBinUUID;
        SimpleGroup g = findGroup(recycleBinUuid);
        if (g == null && isRecycleBinEnabled()) {
            g = newGroup("Recycle Bin");
            getRootGroup().addGroup(g);
            this.keePassFile.meta.recycleBinUUID = g.getUuid();
            this.keePassFile.meta.recycleBinChanged = new Date();
        }
        return g;
    }

    @Override
    public String getName() {
        return keePassFile.meta.databaseName;
    }

    @Override
    public void setName(String s) {
        keePassFile.meta.databaseName = s;
        keePassFile.meta.databaseNameChanged = new Date();
        setDirty(true);
    }

    @Override
    public String getDescription() {
        return keePassFile.meta.databaseDescription;
    }

    @Override
    public void setDescription(String s) {
        keePassFile.meta.databaseDescription = s;
        keePassFile.meta.databaseDescriptionChanged = new Date();
        setDirty(true);
    }

    /**
     * Create an empty underlying KeePassFile instance
     *
     * @return a new database
     * @throws Exception on failure
     */
    private static KeePassFile createEmptyDatabase() throws Exception {
        InputStream inputStream = SimpleDatabase.class.getClassLoader().getResourceAsStream("base.kdbx.xml");
        return getSerializer().read(KeePassFile.class, inputStream);
    }

    /**
     * Load plaintext XML
     *
     * @param inputStream contains the XML
     * @return a new Database
     * @throws Exception on load failure
     */
    public static SimpleDatabase loadXml(InputStream inputStream) throws Exception {
        KeePassFile result =  getSerializer().read(KeePassFile.class, inputStream);
        result.root.group.uuid = UUID.randomUUID();
        return new SimpleDatabase(result);
    }

    /**
     * Load kdbx file
     *
     * @param credentials the credentials to use
     * @param inputStream the encrypted input stream
     * @return a new database
     * @throws Exception on load failure
     */
    public static SimpleDatabase load(Credentials credentials, InputStream inputStream) throws Exception {

        // load the KDBX header and get the inner Kdbx stream
        KdbxHeader kdbxHeader = new KdbxHeader();
        InputStream kdbxInnerStream = KdbxSerializer.createUnencryptedInputStream(credentials, kdbxHeader, inputStream);

        StreamEncryptor streamEncyptor = kdbxHeader.getInnerStreamEncryptor();

        // decrypt the encrypted fields in the inner XML stream
        InputStream plainTextXmlStream = new XmlInputStreamFilter(kdbxInnerStream, new KdbxInputTransformer(streamEncyptor));

        // read the now entirely decrypted stream into database
        KeePassFile result = getSerializer().read(KeePassFile.class, plainTextXmlStream);

        if (kdbxHeader.getVersion() == 3 && !Arrays.equals(result.meta.headerHash.getContent(), kdbxHeader.getHeaderHash())) {
            throw new IllegalStateException("Header Hash Mismatch");
        }

        if (kdbxHeader.getVersion() == 4) {
            int index = 0;
            for (byte[] binary : kdbxHeader.getBinaries()) {
                addBinary(result, binary, index);
                index++;
            }
        }

        return new SimpleDatabase(result);
    }

    public void addBinary(byte[] bytes, Integer index) {
        addBinary(keePassFile, bytes, index);
    }

    public static void addBinary(KeePassFile keePassFile, byte[] bytes, Integer index) {
        // create a new binary to put in the store
        KeePassFile.Binary newBin = new KeePassFile.Binary();
        newBin.setId(index);
        newBin.setValue(Helpers.encodeBase64Content(bytes, true));
        newBin.setCompressed(true);
        if (keePassFile.getBinaries() == null) {
            keePassFile.createBinaries();
        }
        keePassFile.getBinaries().add(newBin);
    }

    /**
     * Save as plaintext XML
     *
     * @param outputStream the destination to save to
     */
    public void save(OutputStream outputStream) {
        try {
            prepareForSave(keePassFile.root.group);

            // and save the database out
            getSerializer().write(this.keePassFile, outputStream);

        } catch (Exception e) {
            throw new IllegalStateException(e);
        }

    }

    @Override
    public void save(Credentials credentials, OutputStream outputStream) throws IOException {
        try {
            // create the stream to accept unencrypted data and output to encrypted
            KdbxHeader kdbxHeader = new KdbxHeader();
            OutputStream kdbxInnerStream = KdbxSerializer.createEncryptedOutputStream(credentials, kdbxHeader, outputStream);

            // the database contains the hash of the headers
            keePassFile.meta.headerHash.setContent(kdbxHeader.getHeaderHash());

            // encrypt the fields in the XML inner stream
            XmlOutputStreamFilter plainTextOutputStream = new XmlOutputStreamFilter(kdbxInnerStream,
                    new KdbxOutputTransformer(new StreamEncryptor.Salsa20(kdbxHeader.getInnerRandomStreamKey())));

            // set up the "protected" attributes of fields that need inner stream encryption
            prepareForSave(keePassFile.root.group);

            // and save the database out
            getSerializer().write(this.keePassFile, plainTextOutputStream);
            plainTextOutputStream.close();
            plainTextOutputStream.await();
            this.setDirty(false);

        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    @Override
    public boolean shouldProtect(String s) {
        return keePassFile.meta.memoryProtection.shouldProtect(s);
    }

    public List<KeePassFile.Binary> getBinaries() {
        return keePassFile.getBinaries();
    }

    /**
     * Utility to get a simple framework persister
     * @return a persister
     * @throws Exception when things get tough
     */
    private static Serializer getSerializer() throws Exception {
        Registry registry = new Registry();
        registry.bind(String.class, EmptyStringConverter.class);
        Strategy strategy = new AnnotationStrategy(new RegistryStrategy(registry));
        return new Persister(strategy);

    }

    /**
     * Utility to add in back links to parent group and database
     *
     * @param parent the group to start from
     */
    private static void fixUp(SimpleGroup parent){
        for (SimpleGroup group: parent.group) {
            group.parent = parent;
            group.database = parent.database;
            fixUp(group);
        }
        for (SimpleEntry entry: parent.entry) {
            entry.database = parent.database;
            entry.parent = parent;
        }
    }

    /**
     * Utility to mark fields that need to be encrypted and vice versa
     *
     * @param parent the group to start from
     */
    private static void prepareForSave(SimpleGroup parent){
        for (SimpleGroup group: parent.group) {
            prepareForSave(group);
        }
        for (SimpleEntry entry: parent.entry) {
            for (EntryClasses.StringProperty property : entry.string) {
                boolean shouldProtect = parent.database.shouldProtect(property.getKey());
                property.getValue().setProtected(shouldProtect);
            }
        }
    }
}


text:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DatePipePipe } from './date-pipe.pipe';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [DatePipePipe],
  exports: [DatePipePipe]
})
export class DatePipeModule { }


text:
require 'spec_helper'

describe CCML::Tag::VoteTag do
  context "'index' method" do
    before(:each) do
      @vote1 = FactoryGirl.create(:vote, :title => 'Vote1title')
      @vote2 = FactoryGirl.create(:vote, :title => 'Vote2title')
      @url = "http://www.theciviccommons.com/whatever/url"
      @vote_title1 = Regexp.new(@vote1.title)
      @vote_title2 = Regexp.new(@vote2.title)
    end

    it "accepts an id as opts id" do
      tag = CCML::Tag::VoteTag.new({id: @vote1.id}, @url)
      tag.index.should =~ @vote_title1
    end

    it "accepts limit and correctly displays the limit" do
      tag = CCML::Tag::VoteTag.new({id: @vote1.id, limit: 1}, @url)
      tag.index.should =~ @vote_title1
      tag.index.should_not =~ @vote_title2
    end

    it "accepts no argument" do
      tag = CCML::Tag::VoteTag.new({}, @url)
      tag.index.should =~ @vote_title1
      tag.index.should =~ @vote_title2

    end

  end

end


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.7"/>
<title>Documentation: Member List</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>

<link href="navtree.css" rel="stylesheet" type="text/css"/>

<link href="doxygen.css" rel="stylesheet" type="text/css" />

Documentation

For Arduino users

Main Page

Related Pages

Modules

Classes

Files

Examples

Class List

Class Index

Class Hierarchy

Class Members

NexNumber Member List

This is the complete list of members for NexNumber, including all inherited members.

attachPop(NexTouchEventCb pop, void *ptr=NULL)	NexTouch

attachPush(NexTouchEventCb push, void *ptr=NULL)	NexTouch

detachPop(void)	NexTouch

detachPush(void)	NexTouch

Get_background_color_bco(uint32_t *number)	NexNumber

Get_background_crop_picc(uint32_t *number)	NexNumber

Get_background_image_pic(uint32_t *number)	NexNumber

Get_font_color_pco(uint32_t *number)	NexNumber

Get_number_lenth(uint32_t *number)	NexNumber

Get_place_xcen(uint32_t *number)	NexNumber

Get_place_ycen(uint32_t *number)	NexNumber

getFont(uint32_t *number)	NexNumber

getValue(uint32_t *number)	NexNumber

NexNumber(uint8_t pid, uint8_t cid, const char *name)	NexNumber

NexObject(uint8_t pid, uint8_t cid, const char *name)	NexObject

NexTouch(uint8_t pid, uint8_t cid, const char *name)	NexTouch

printObjInfo(void)	NexObject

Set_background_color_bco(uint32_t number)	NexNumber

Set_background_crop_picc(uint32_t number)	NexNumber

Set_background_image_pic(uint32_t number)	NexNumber

Set_font_color_pco(uint32_t number)	NexNumber

Set_number_lenth(uint32_t number)	NexNumber

Set_place_xcen(uint32_t number)	NexNumber

Set_place_ycen(uint32_t number)	NexNumber

setFont(uint32_t number)	NexNumber

setValue(uint32_t number)	NexNumber

Generated on Tue Oct 11 2016 14:29:37 for Documentation by

     1.8.7


text:
'use strict';
var exporter = require('../lib/exporter.js');
var assert = require('assert');

describe('exporter', function () {

  beforeEach(function () {
    exporter.interpolate = {};
    exporter.config = {};
  });

  it('should load', function () {
    assert(exporter() !== undefined);
  });

  it('should allow data to be passed directly in', function (done) {
    var mock = [1, 2, 3];

    exporter(mock)._toObject(function (data) {
      assert.deepEqual(data, mock, data);
      done();
    }).catch(function (error) {
      console.log('error', error);
      done(error);
    });
  });

  it('should allow us to catch errors', function () {
    var mock = [1, 2, 3];

    return exporter(mock).then(function (data) {
      throw new Error('ok');
    }).catch(function (error) {
      assert.ok(error.message === 'ok', error.message);
    }).__state;
  })

  it('should support adding plugins', function (done) {
    exporter.plugins.foo = function () {
      assert(true);
      done();
      return this;
    };
    exporter().foo();
  });

  it('should have a global and local config', function () {
    exporter.config = {
      foo: 10,
      bar: 10,
    };

    // config merges with global config, it doesn't overwrite
    var e = exporter().config({
      foo: 20,
    });

    // modified exporter object don't override global values
    assert(e.settings.foo === 20, 'foo actual: ' + JSON.stringify(e.settings));
    assert(e.settings.bar === 10, 'bar actual: ' + JSON.stringify(e.settings));
    // new exporter objects have foo = 10
    assert(exporter().settings.foo === 10, 'top.foo actual: ' + JSON.stringify(exporter().settings));
  });

});


text:
/* Common header used to define underlying types for typedefs that
 * must appear in multiple headers independently.
 */
#ifndef ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_SYS_XTYPES_H_
#define ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_SYS_XTYPES_H_

#include <stdint.h>

typedef int64_t _TIME_T_;
typedef int32_t _SUSECONDS_T_;

#endif /* ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_SYS_XTYPES_H_ */