private static ServiceClientContentStoreConfiguration CreateConfiguration(
     string cacheName,
     string scenario,
     ServiceConfiguration serviceConfiguration,
     uint retryIntervalSeconds = DefaultRetryIntervalSeconds,
     uint retryCount           = DefaultRetryCount)
 {
     return(new ServiceClientContentStoreConfiguration(
                cacheName,
                ServiceClientRpcConfiguration.CreateGrpc((int)serviceConfiguration.GrpcPort),
                scenario)
     {
         RetryCount = retryCount,
         RetryIntervalSeconds = retryIntervalSeconds,
     });
 }
Example #2
0
        internal static BuildXL.Cache.MemoizationStore.Interfaces.Caches.ICache CreateBuildCacheCache <T>(T cacheConfig, ILogger logger, string pat = null) where T : BuildCacheCacheConfig
        {
            // TODO: Remove check when all clients are updated with unified Dedup flag
            if (ContentHashingUtilities.HashInfo.HashType == HashType.DedupNodeOrChunk ^ cacheConfig.UseDedupStore)
            {
                var store = cacheConfig.UseDedupStore ? "DedupStore" : "BlobStore";
                throw new ArgumentException($"HashType {ContentHashingUtilities.HashInfo.HashType} cannot be used with {store}");
            }

            string credentialProviderPath = Environment.GetEnvironmentVariable(CredentialProvidersPathEnvVariable);

            if (!string.IsNullOrWhiteSpace(credentialProviderPath))
            {
                logger.Debug($"Credential providers path specified: {credentialProviderPath}");
            }
            else
            {
                logger.Debug("Using current user's credentials for obtaining AAD token");
            }

            VssCredentialsFactory credentialsFactory;

#if !FEATURE_CORECLR
            credentialsFactory = new VssCredentialsFactory(new VsoCredentialHelper(s => logger.Debug(s)));
#else
            var secPat = new SecureString();
            if (!string.IsNullOrWhiteSpace(pat))
            {
                foreach (char c in pat)
                {
                    secPat.AppendChar(c);
                }
            }
            else
            {
                throw new ArgumentException("PAT must be supplied when running with CoreCLR");
            }

            credentialsFactory = new VssCredentialsFactory(new VssBasicCredential(new NetworkCredential(string.Empty, secPat)));
#endif

            logger.Diagnostic("Creating BuildCacheCache factory");
            var fileSystem = new PassThroughFileSystem(logger);

            // TODO: Once write-behind is implemented send a contentstorefunc down to the create.
            Func <IContentStore> writeThroughStore = null;
            if (!string.IsNullOrWhiteSpace(cacheConfig.CacheName))
            {
                ServiceClientRpcConfiguration rpcConfiguration;
                if (cacheConfig.GrpcPort != 0)
                {
                    rpcConfiguration = ServiceClientRpcConfiguration.CreateGrpc((int)cacheConfig.GrpcPort);
                }
                else
                {
                    var factory    = new MemoryMappedFileGrpcPortSharingFactory(logger, cacheConfig.GrpcPortFileName);
                    var portReader = factory.GetPortReader();
                    var port       = portReader.ReadPort();

                    rpcConfiguration = new ServiceClientRpcConfiguration(port);
                }

                writeThroughStore = () =>
                                    new ServiceClientContentStore(
                    logger,
                    fileSystem,
                    cacheConfig.CacheName,
                    rpcConfiguration,
                    cacheConfig.ConnectionRetryIntervalSeconds,
                    cacheConfig.ConnectionRetryCount,
                    scenario: cacheConfig.ScenarioName);
            }

            BuildCacheServiceConfiguration buildCacheServiceConfiguration = cacheConfig.AsBuildCacheServiceConfigurationFile();
            return(BuildCacheCacheFactory.Create(fileSystem, logger, credentialsFactory, buildCacheServiceConfiguration, writeThroughStore));
        }
Example #3
0
        /// <inheritdoc />
        protected override ICache CreateCache(DisposableDirectory testDirectory)
        {
            var backendCacheDirectory = testDirectory.Path / "Backend";

            FileSystem.CreateDirectory(backendCacheDirectory);

            var namedCacheRoots = new Dictionary <string, AbsolutePath> {
                [CacheName] = backendCacheDirectory / "Root"
            };

            var grpcPort            = PortExtensions.GetNextAvailablePort();
            var serverConfiguration = new LocalServerConfiguration(backendCacheDirectory, namedCacheRoots, grpcPort)
            {
                GrpcPortFileName = null, // Port is well known at configuration time, no need to expose it.
            };
            var serviceClientConfiguration = new ServiceClientContentStoreConfiguration(CacheName, ServiceClientRpcConfiguration.CreateGrpc(serverConfiguration.GrpcPort), "Scenario-" + Guid.NewGuid());
            Func <AbsolutePath, ICache> contentStoreFactory = CreateBackendCache;
            var serviceClient = new TestInProcessServiceClientCache(Logger, FileSystem, contentStoreFactory, serverConfiguration, serviceClientConfiguration);

            return(serviceClient);
        }
        /// <inheritdoc />
        public async Task <Possible <ICache, Failure> > InitializeCacheAsync(ICacheConfigData cacheData, Guid activityId)
        {
            Contract.Requires(cacheData != null);

            var possibleCacheConfig = cacheData.Create <Config>();

            if (!possibleCacheConfig.Succeeded)
            {
                return(possibleCacheConfig.Failure);
            }

            Config cacheConfig = possibleCacheConfig.Result;

            try
            {
                Contract.Assert(cacheConfig.CacheName != null);
                var logPath = new AbsolutePath(cacheConfig.MetadataLogPath);
                var logger  = new DisposeLogger(() => new EtwFileLog(logPath.Path, cacheConfig.CacheId), cacheConfig.LogFlushIntervalSeconds);

                logger.Debug($"Creating CASaaS backed LocalCache using cache name: {cacheConfig.CacheName}");

                ServiceClientRpcConfiguration rpcConfiguration;
                if (cacheConfig.GrpcPort != 0)
                {
                    rpcConfiguration = ServiceClientRpcConfiguration.CreateGrpc((int)cacheConfig.GrpcPort);
                }
                else
                {
                    var factory    = new MemoryMappedFileGrpcPortSharingFactory(logger, cacheConfig.GrpcPortFileName);
                    var portReader = factory.GetPortReader();
                    var port       = portReader.ReadPort();

                    rpcConfiguration = new ServiceClientRpcConfiguration(port);
                }

                var rootPath   = new AbsolutePath(cacheConfig.MetadataRootPath);
                var memoConfig = new SQLiteMemoizationStoreConfiguration(rootPath)
                {
                    MaxRowCount                  = cacheConfig.MaxStrongFingerprints,
                    BackupDatabase               = cacheConfig.BackupLKGCache,
                    VerifyIntegrityOnStartup     = cacheConfig.CheckCacheIntegrityOnStartup,
                    SingleInstanceTimeoutSeconds = (int)cacheConfig.SingleInstanceTimeoutInSeconds
                };

                if (!string.IsNullOrEmpty(cacheConfig.SynchronizationMode))
                {
                    memoConfig.SyncMode = (SynchronizationMode)Enum.Parse(typeof(SynchronizationMode), cacheConfig.SynchronizationMode, ignoreCase: true);
                }

                var localCache = new LocalCache(
                    logger,
                    cacheConfig.CacheName,
                    rootPath,
                    rpcConfiguration,
                    cacheConfig.ConnectionRetryIntervalSeconds,
                    cacheConfig.ConnectionRetryCount,
                    memoConfig,
                    scenarioName: cacheConfig.ScenarioName);

                var statsFilePath = new AbsolutePath(logPath.Path + ".stats");
                var cache         = new MemoizationStoreAdapterCache(cacheConfig.CacheId, localCache, logger, statsFilePath);

                var startupResult = await cache.StartupAsync();

                if (!startupResult.Succeeded)
                {
                    logger.Error($"Error while initializing the cache [{cacheConfig.CacheId}]. Failure: {startupResult.Failure}");
                    return(startupResult.Failure);
                }

                logger.Debug("Successfully started CloudStoreLocalCacheService client.");
                return(cache);
            }
            catch (Exception e)
            {
                return(new CacheConstructionFailure(cacheConfig.CacheId, e));
            }
        }