text:
'use strict';

var logger = require('../logger/logger').logger;
var assert = require('assert-plus');
var festivals = require('../festivals');
var extend = require('util')._extend;
var myRestifyApi = require('my-restify-api');
var BadRequestError = myRestifyApi.error.BadRequestError;
var keen = require('../keen');
var merger = require('../merger');
var cache = require('../cache');
var categoryBuilders = require('../domain/builder/categoryBuilders');

var festivalsModel = require('festivals-model');
var CategoriesCollectionResponseBuilder = festivalsModel.model.categoriesCollectionResponse.CategoriesCollectionResponseBuilder;
var SearchFestivalCategoriesRequestBuilder = festivalsModel.model.searchFestivalCategoriesRequest.SearchFestivalCategoriesRequestBuilder;
var FestivalCategoryNotFoundError = festivalsModel.error.FestivalCategoryNotFoundError;

var createFestivalCategoryV1 = function createFestivalCategoryV1(req, res, next) {
  var category = null;

  try {
    assert.object(req.params, 'params');
    assert.string(req.params.id, 'id');
    category = categoryBuilders.buildCategoryDomain(req.params.id, req.params, true);
  }
  catch (e) {
    logger.warn('assert error: ', e);
    return next(new BadRequestError(e.message));
  }

  var id = req.params.id;

  return festivals.createFestivalCategory(id, category, {}, function (err/*, result*/) {
    if (err) {
      keen.errorCategory('create', req.authorization.bearer, req.params, err);
    }
    next.ifError(err);

    res.send(201, categoryBuilders.buildCategoryResponse(category));
    next();

    return cache.purge(req.authorization.credentials, '/api/festivals/' + id);
  });
};

var updateFestivalCategoryV1 = function updateFestivalCategoryV1(req, res, next) {
  try {
    assert.object(req.params, 'params');
    assert.string(req.params.id, 'id');
    assert.string(req.params['category.id'], 'category.id');
  }
  catch (e) {
    logger.warn('assert error: ', e);
    return next(new BadRequestError(e.message));
  }

  var id = req.params.id;
  var cid = req.params['category.id'];

  return festivals.getFestivalCategory(id, cid, {}, function (err, category) {

    if (err) {
      keen.errorCategory('update', req.authorization.bearer, req.params, err);
    }

    next.ifError(err);

    if (!category) {
      return next(new FestivalCategoryNotFoundError('Festival category not found'));
    }

    merger.merge(category, req.params);
    category.id = cid; //id came from festival so we need to override it

    var newCategory = categoryBuilders.buildCategoryDomain(id, category, false);

    return festivals.updateFestivalCategory(id, cid, newCategory, {}, function (errCategory/*, result*/) {
      if (errCategory) {
        keen.errorCategory('update', req.authorization.bearer, req.params, errCategory);
      }
      next.ifError(errCategory);
      res.send(200, categoryBuilders.buildCategoryResponse(extend(category, newCategory)));
      next();

      return cache.purge(req.authorization.credentials, '/api/festivals/' + id);
    });
  });
};

var getFestivalCategoriesV1 = function getFestivalCategoriesV1(req, res, next) {
  try {
    assert.object(req.params, 'params');
    assert.string(req.params.id, 'id');

    assert.optionalString(req.params.name, 'name');
    assert.optionalString(req.params.parent, 'parent');
    assert.optionalString(req.params.updatedAt, 'updatedAt');
    assert.optionalString(req.params.limit, 'limit');
    assert.optionalString(req.params.offset, 'offset');
    assert.optionalString(req.params.sort, 'sort');
  }
  catch (e) {
    logger.warn('assert error: ', e);
    return next(new BadRequestError(e.message));
  }

  var id = req.params.id;
  var name = req.params.name;
  var parent = req.params.parent;
  var updatedAt = req.params.updatedAt || null;
  var limit = ~~req.params.limit || 100;
  var offset = ~~req.params.offset;
  var sort = req.params.sort;

  var search = new SearchFestivalCategoriesRequestBuilder()
    .withName(name)
    .withParent(parent)
    .withUpdatedAt(updatedAt)
    .withLimit(limit)
    .withOffset(offset)
    .withSort(sort)
    .build();

  return festivals.getFestivalCategories(id, search, {}, function (err, data) {
    if (err) {
      keen.errorCategory('search', req.authorization.bearer, req.params, err);
    }

    next.ifError(err);

    var categories = data.categories.map(function (category) {
      return categoryBuilders.buildCategoryResponse(category);
    });

    var response = new CategoriesCollectionResponseBuilder()
      .withTotal(data.total)
      .withCategories(categories)
      .build();

    res.send(200, response);
    next();

    return keen.categoriesSearch(req.authorization.bearer, req.params);
  });
};

var getFestivalCategoryV1 = function getFestivalCategoryV1(req, res, next) {
  try {
    assert.object(req.params, 'params');
    assert.string(req.params.id, 'id');
    assert.string(req.params['category.id'], 'category.id');
  }
  catch (e) {
    logger.warn('assert error: ', e);
    return next(new BadRequestError(e.message));
  }

  var id = req.params.id;
  var cid = req.params['category.id'];

  return festivals.getFestivalCategory(id, cid, {}, function (err, category) {
    if (err) {
      keen.errorCategory('get', req.authorization.bearer, req.params, err);
    }
    next.ifError(err);

    if (!category) {
      return next(new FestivalCategoryNotFoundError('Festival category not found'));
    }

    res.send(200, categoryBuilders.buildCategoryResponse(category));
    return next();
  });
};

var deleteFestivalCategoryV1 = function deleteFestivalCategoryV1(req, res, next) {
  try {
    assert.object(req.params, 'params');
    assert.string(req.params.id, 'id');
    assert.string(req.params['category.id'], 'category.id');
  }
  catch (e) {
    logger.warn('assert error: ', e);
    return next(new BadRequestError(e.message));
  }

  var id = req.params.id;
  var cid = req.params['category.id'];

  return festivals.getFestivalCategory(id, cid, {}, function (err, category) {
    if (err) {
      keen.errorCategory('get', req.authorization.bearer, req.params, err);
    }
    next.ifError(err);

    if (!category) {
      return next(new FestivalCategoryNotFoundError('Festival category not found'));
    }

    return festivals.deleteFestivalCategory(id, cid, {}, function (errCategory/*, result*/) {
      if (errCategory) {
        keen.errorCategory('delete', req.authorization.bearer, req.params, errCategory);
      }
      next.ifError(errCategory);

      res.send(204, '');
      next();

      return cache.purge(req.authorization.credentials, '/api/festivals/' + id);
    });
  });
};

module.exports = {
  createFestivalCategoryV1: createFestivalCategoryV1,
  updateFestivalCategoryV1: updateFestivalCategoryV1,
  getFestivalCategoriesV1: getFestivalCategoriesV1,
  getFestivalCategoryV1: getFestivalCategoryV1,
  deleteFestivalCategoryV1: deleteFestivalCategoryV1
};


text:
const set = require('lodash/set');
const find = require('lodash/find');
const config = require('../../src/config').get('/');

const { audience } = config.users;

const users = [
  { id: '1000000000', alias: 'admin' },
  { id: '2000000000', alias: 'free' },
  { id: '3000000000', alias: 'professional' },
  { id: '4000000000', alias: 'adminLimit' },
];

const mockPlan = (value) => set({}, 'meta.embeddings.value', value);

const mockMetadata = (alias, attributes) => ({
  id: find(users, { alias }).id,
  [audience]: attributes,
});

const mockInternalData = (alias) => ({
  id: find(users, { alias }).id,
});

const plans = {
  free: mockPlan(1),
  professional: mockPlan(10),
};

const metadata = {
  free: mockMetadata('free', { plan: 'free', roles: [] }),
  professional: mockMetadata('professional', { plan: 'professional', roles: [] }),
  admin: mockMetadata('admin', { plan: 'professional', roles: ['admin'] }),
  adminLimit: mockMetadata('adminLimit', { plan: 'free', roles: ['admin'] }),
};

const internals = {
  free: mockInternalData('free'),
  professional: mockInternalData('professional'),
  admin: mockInternalData('admin'),
  adminLimit: mockMetadata('adminLimit'),
};

const uploadedFiles = {
  free: 10,
  professional: 1,
  admin: 1,
  adminLimit: 10,
};

module.exports = {
  users,
  plans,
  metadata,
  internals,
  uploadedFiles,
};

redpajama_set_name: RedPajamaGithub


text:
package com.aspose.pdf.cloud.text;

import java.nio.file.Path;

import com.aspose.pdf.api.PdfApi;
import com.aspose.pdf.cloud.examples.Configuration;
import com.aspose.pdf.cloud.examples.Utils;
import com.aspose.pdf.cloud.images.ExtractImageDefaultSizeExample;
import com.aspose.pdf.model.TextItemsResponse;
import com.aspose.storage.api.StorageApi;

public class GetSegmentCountExample {

	public static void main(String[] args) {
		String fileName = "sample-input.pdf";
		int pageNumber = 1;
		int fragmentNumber = 1;
		String withEmpty = "";
		String storage = "";
		String folder = "";
		Path inputFile = Utils.getPath(ExtractImageDefaultSizeExample.class, fileName);

		try
		{
			StorageApi storageApi = new StorageApi(Configuration.apiKey, Configuration.appSID, true);
			// Instantiate Aspose Words API SDK
			PdfApi pdfApi = new PdfApi(Configuration.apiKey, Configuration.appSID, true);
		    // Upload source file to aspose cloud storage
		    storageApi.PutCreate(fileName, "", "", inputFile.toFile());
		    // Invoke Aspose.PDF Cloud SDK API to get segment count from a pdf fragment
		    TextItemsResponse apiResponse = pdfApi.GetSegments(fileName, pageNumber, fragmentNumber, withEmpty, storage, folder);
		    if (apiResponse != null && apiResponse.getStatus().equals("OK"))
		    {
		    	System.out.println("Segment Count :" + apiResponse.getTextItems().getList().size());
		    }
		}
		catch (Exception ex)
		{
		    ex.printStackTrace();

		}
	}

}


text:
<?php

require_once 'index/xfIndex.interface.php';
require_once 'index/xfIndexCommon.class.php';
require_once 'index/xfIndexSingle.class.php';
require_once 'service/xfServiceRegistry.class.php';
require_once 'service/xfService.class.php';
require_once 'service/xfIdentifier.interface.php';
require_once 'service/xfPropelIdentifier.class.php';
require_once 'mock/engine/xfMockEngine.class.php';
require_once 'document/xfDocument.class.php';
require_once 'document/xfField.class.php';
require_once 'document/xfFieldValue.class.php';
require_once 'document/xfBuilder.interface.php';
require_once 'document/xfPropelBuilder.class.php';

class xfMockPropelIndex extends xfIndexSingle
{
  protected function configure()
  {
    $this->setEngine(new xfMockEngine);

    $service = new xfService(new xfPropelIdentifier('MyModel'));
    $service->addBuilder(new xfPropelBuilder(array(
                                                    new xfField('name', xfField::KEYWORD),
                                                    new xfField('age', xfField::KEYWORD)
    )));

    $this->getServiceRegistry()->register($service);
  }
}


text:
using System;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Data.SchemaRegistry.Models;

namespace Azure.Data.SchemaRegistry
{
    internal partial class SchemaRestClient
    {
        private readonly HttpPipeline _pipeline;
        private readonly string _endpoint;
        private readonly string _apiVersion;

        /// <summary> The ClientDiagnostics is used to provide tracing support for the client library. </summary>
        internal ClientDiagnostics ClientDiagnostics { get; }

        /// <summary> Initializes a new instance of SchemaRestClient. </summary>
        /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param>
        /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param>
        /// <param name="endpoint"> The Schema Registry service endpoint, for example my-namespace.servicebus.windows.net. </param>
        /// <param name="apiVersion"> Api Version. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="clientDiagnostics"/>, <paramref name="pipeline"/>, <paramref name="endpoint"/> or <paramref name="apiVersion"/> is null. </exception>
        public SchemaRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string endpoint, string apiVersion = "2021-10")
        {
            ClientDiagnostics = clientDiagnostics ?? throw new ArgumentNullException(nameof(clientDiagnostics));
            _pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline));
            _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
            _apiVersion = apiVersion ?? throw new ArgumentNullException(nameof(apiVersion));
        }

        internal HttpMessage CreateGetByIdRequest(string id)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;
            request.Method = RequestMethod.Get;
            var uri = new RawRequestUriBuilder();
            uri.AppendRaw("https://", false);
            uri.AppendRaw(_endpoint, false);
            uri.AppendPath("/$schemaGroups/$schemas/", false);
            uri.AppendPath(id, true);
            uri.AppendQuery("api-version", _apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json; serialization=Avro");
            return message;
        }

        /// <summary> Get a registered schema by its unique ID reference. </summary>
        /// <param name="id"> References specific schema in registry namespace. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="id"/> is null. </exception>
        /// <remarks> Gets a registered schema by its unique ID.  Azure Schema Registry guarantees that ID is unique within a namespace. Operation response type is based on serialization of schema requested. </remarks>
        public async Task<ResponseWithHeaders<Stream, SchemaGetByIdHeaders>> GetByIdAsync(string id, CancellationToken cancellationToken = default)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            using var message = CreateGetByIdRequest(id);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
            var headers = new SchemaGetByIdHeaders(message.Response);
            switch (message.Response.Status)
            {
                case 200:
                    {
                        var value = message.ExtractResponseContent();
                        return ResponseWithHeaders.FromValue(value, headers, message.Response);
                    }
                default:
                    throw await ClientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false);
            }
        }

        /// <summary> Get a registered schema by its unique ID reference. </summary>
        /// <param name="id"> References specific schema in registry namespace. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="id"/> is null. </exception>
        /// <remarks> Gets a registered schema by its unique ID.  Azure Schema Registry guarantees that ID is unique within a namespace. Operation response type is based on serialization of schema requested. </remarks>
        public ResponseWithHeaders<Stream, SchemaGetByIdHeaders> GetById(string id, CancellationToken cancellationToken = default)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            using var message = CreateGetByIdRequest(id);
            _pipeline.Send(message, cancellationToken);
            var headers = new SchemaGetByIdHeaders(message.Response);
            switch (message.Response.Status)
            {
                case 200:
                    {
                        var value = message.ExtractResponseContent();
                        return ResponseWithHeaders.FromValue(value, headers, message.Response);
                    }
                default:
                    throw ClientDiagnostics.CreateRequestFailedException(message.Response);
            }
        }

        internal HttpMessage CreateGetVersionsRequest(string groupName, string schemaName)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;
            request.Method = RequestMethod.Get;
            var uri = new RawRequestUriBuilder();
            uri.AppendRaw("https://", false);
            uri.AppendRaw(_endpoint, false);
            uri.AppendPath("/$schemaGroups/", false);
            uri.AppendPath(groupName, true);
            uri.AppendPath("/schemas/", false);
            uri.AppendPath(schemaName, true);
            uri.AppendPath("/versions", false);
            uri.AppendQuery("api-version", _apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            return message;
        }

        /// <summary> Get list schema versions. </summary>
        /// <param name="groupName"> Schema group under which schema is registered.  Group's serialization type should match the serialization type specified in the request. </param>
        /// <param name="schemaName"> Name of schema. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="groupName"/> or <paramref name="schemaName"/> is null. </exception>
        /// <remarks> Gets the list of all versions of one schema. </remarks>
        public async Task<Response<SchemaVersions>> GetVersionsAsync(string groupName, string schemaName, CancellationToken cancellationToken = default)
        {
            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }
            if (schemaName == null)
            {
                throw new ArgumentNullException(nameof(schemaName));
            }

            using var message = CreateGetVersionsRequest(groupName, schemaName);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
            switch (message.Response.Status)
            {
                case 200:
                    {
                        SchemaVersions value = default;
                        using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);
                        value = SchemaVersions.DeserializeSchemaVersions(document.RootElement);
                        return Response.FromValue(value, message.Response);
                    }
                default:
                    throw await ClientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false);
            }
        }

        /// <summary> Get list schema versions. </summary>
        /// <param name="groupName"> Schema group under which schema is registered.  Group's serialization type should match the serialization type specified in the request. </param>
        /// <param name="schemaName"> Name of schema. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="groupName"/> or <paramref name="schemaName"/> is null. </exception>
        /// <remarks> Gets the list of all versions of one schema. </remarks>
        public Response<SchemaVersions> GetVersions(string groupName, string schemaName, CancellationToken cancellationToken = default)
        {
            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }
            if (schemaName == null)
            {
                throw new ArgumentNullException(nameof(schemaName));
            }

            using var message = CreateGetVersionsRequest(groupName, schemaName);
            _pipeline.Send(message, cancellationToken);
            switch (message.Response.Status)
            {
                case 200:
                    {
                        SchemaVersions value = default;
                        using var document = JsonDocument.Parse(message.Response.ContentStream);
                        value = SchemaVersions.DeserializeSchemaVersions(document.RootElement);
                        return Response.FromValue(value, message.Response);
                    }
                default:
                    throw ClientDiagnostics.CreateRequestFailedException(message.Response);
            }
        }

        internal HttpMessage CreateGetSchemaVersionRequest(string groupName, string schemaName, int schemaVersion)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;
            request.Method = RequestMethod.Get;
            var uri = new RawRequestUriBuilder();
            uri.AppendRaw("https://", false);
            uri.AppendRaw(_endpoint, false);
            uri.AppendPath("/$schemaGroups/", false);
            uri.AppendPath(groupName, true);
            uri.AppendPath("/schemas/", false);
            uri.AppendPath(schemaName, true);
            uri.AppendPath("/versions/", false);
            uri.AppendPath(schemaVersion, true);
            uri.AppendQuery("api-version", _apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            return message;
        }

        /// <summary> Get specific schema versions. </summary>
        /// <param name="groupName"> Schema group under which schema is registered.  Group's serialization type should match the serialization type specified in the request. </param>
        /// <param name="schemaName"> Name of schema. </param>
        /// <param name="schemaVersion"> Version number of specific schema. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="groupName"/> or <paramref name="schemaName"/> is null. </exception>
        /// <remarks> Gets one specific version of one schema. </remarks>
        public async Task<ResponseWithHeaders<Stream, SchemaGetSchemaVersionHeaders>> GetSchemaVersionAsync(string groupName, string schemaName, int schemaVersion, CancellationToken cancellationToken = default)
        {
            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }
            if (schemaName == null)
            {
                throw new ArgumentNullException(nameof(schemaName));
            }

            using var message = CreateGetSchemaVersionRequest(groupName, schemaName, schemaVersion);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
            var headers = new SchemaGetSchemaVersionHeaders(message.Response);
            switch (message.Response.Status)
            {
                case 200:
                    {
                        var value = message.ExtractResponseContent();
                        return ResponseWithHeaders.FromValue(value, headers, message.Response);
                    }
                default:
                    throw await ClientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false);
            }
        }

        /// <summary> Get specific schema versions. </summary>
        /// <param name="groupName"> Schema group under which schema is registered.  Group's serialization type should match the serialization type specified in the request. </param>
        /// <param name="schemaName"> Name of schema. </param>
        /// <param name="schemaVersion"> Version number of specific schema. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="groupName"/> or <paramref name="schemaName"/> is null. </exception>
        /// <remarks> Gets one specific version of one schema. </remarks>
        public ResponseWithHeaders<Stream, SchemaGetSchemaVersionHeaders> GetSchemaVersion(string groupName, string schemaName, int schemaVersion, CancellationToken cancellationToken = default)
        {
            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }
            if (schemaName == null)
            {
                throw new ArgumentNullException(nameof(schemaName));
            }

            using var message = CreateGetSchemaVersionRequest(groupName, schemaName, schemaVersion);
            _pipeline.Send(message, cancellationToken);
            var headers = new SchemaGetSchemaVersionHeaders(message.Response);
            switch (message.Response.Status)
            {
                case 200:
                    {
                        var value = message.ExtractResponseContent();
                        return ResponseWithHeaders.FromValue(value, headers, message.Response);
                    }
                default:
                    throw ClientDiagnostics.CreateRequestFailedException(message.Response);
            }
        }

        internal HttpMessage CreateQueryIdByContentRequest(string groupName, string schemaName, string contentType, Stream schemaContent)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;
            request.Method = RequestMethod.Post;
            var uri = new RawRequestUriBuilder();
            uri.AppendRaw("https://", false);
            uri.AppendRaw(_endpoint, false);
            uri.AppendPath("/$schemaGroups/", false);
            uri.AppendPath(groupName, true);
            uri.AppendPath("/schemas/", false);
            uri.AppendPath(schemaName, true);
            uri.AppendPath(":get-id", false);
            uri.AppendQuery("api-version", _apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Content-Type", contentType);
            request.Content = RequestContent.Create(schemaContent);
            return message;
        }

        /// <summary> Get ID for existing schema. </summary>
        /// <param name="groupName"> Schema group under which schema is registered.  Group's serialization type should match the serialization type specified in the request. </param>
        /// <param name="schemaName"> Name of schema. </param>
        /// <param name="contentType"> Content type of the schema. </param>
        /// <param name="schemaContent"> String representation (UTF-8) of the registered schema. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="groupName"/>, <paramref name="schemaName"/>, <paramref name="contentType"/> or <paramref name="schemaContent"/> is null. </exception>
        /// <remarks> Gets the ID referencing an existing schema within the specified schema group, as matched by schema content comparison. </remarks>
        public async Task<ResponseWithHeaders<SchemaQueryIdByContentHeaders>> QueryIdByContentAsync(string groupName, string schemaName, string contentType, Stream schemaContent, CancellationToken cancellationToken = default)
        {
            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }
            if (schemaName == null)
            {
                throw new ArgumentNullException(nameof(schemaName));
            }
            if (contentType == null)
            {
                throw new ArgumentNullException(nameof(contentType));
            }
            if (schemaContent == null)
            {
                throw new ArgumentNullException(nameof(schemaContent));
            }

            using var message = CreateQueryIdByContentRequest(groupName, schemaName, contentType, schemaContent);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
            var headers = new SchemaQueryIdByContentHeaders(message.Response);
            switch (message.Response.Status)
            {
                case 204:
                    return ResponseWithHeaders.FromValue(headers, message.Response);
                default:
                    throw await ClientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false);
            }
        }

        /// <summary> Get ID for existing schema. </summary>
        /// <param name="groupName"> Schema group under which schema is registered.  Group's serialization type should match the serialization type specified in the request. </param>
        /// <param name="schemaName"> Name of schema. </param>
        /// <param name="contentType"> Content type of the schema. </param>
        /// <param name="schemaContent"> String representation (UTF-8) of the registered schema. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="groupName"/>, <paramref name="schemaName"/>, <paramref name="contentType"/> or <paramref name="schemaContent"/> is null. </exception>
        /// <remarks> Gets the ID referencing an existing schema within the specified schema group, as matched by schema content comparison. </remarks>
        public ResponseWithHeaders<SchemaQueryIdByContentHeaders> QueryIdByContent(string groupName, string schemaName, string contentType, Stream schemaContent, CancellationToken cancellationToken = default)
        {
            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }
            if (schemaName == null)
            {
                throw new ArgumentNullException(nameof(schemaName));
            }
            if (contentType == null)
            {
                throw new ArgumentNullException(nameof(contentType));
            }
            if (schemaContent == null)
            {
                throw new ArgumentNullException(nameof(schemaContent));
            }

            using var message = CreateQueryIdByContentRequest(groupName, schemaName, contentType, schemaContent);
            _pipeline.Send(message, cancellationToken);
            var headers = new SchemaQueryIdByContentHeaders(message.Response);
            switch (message.Response.Status)
            {
                case 204:
                    return ResponseWithHeaders.FromValue(headers, message.Response);
                default:
                    throw ClientDiagnostics.CreateRequestFailedException(message.Response);
            }
        }

        internal HttpMessage CreateRegisterRequest(string groupName, string schemaName, string contentType, Stream schemaContent)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;
            request.Method = RequestMethod.Put;
            var uri = new RawRequestUriBuilder();
            uri.AppendRaw("https://", false);
            uri.AppendRaw(_endpoint, false);
            uri.AppendPath("/$schemaGroups/", false);
            uri.AppendPath(groupName, true);
            uri.AppendPath("/schemas/", false);
            uri.AppendPath(schemaName, true);
            uri.AppendQuery("api-version", _apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Content-Type", contentType);
            request.Content = RequestContent.Create(schemaContent);
            return message;
        }

        /// <summary> Register new schema. </summary>
        /// <param name="groupName"> Schema group under which schema should be registered.  Group's serialization type should match the serialization type specified in the request. </param>
        /// <param name="schemaName"> Name of schema. </param>
        /// <param name="contentType"> Content type of the schema. </param>
        /// <param name="schemaContent"> String representation (UTF-8) of the schema being registered. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="groupName"/>, <paramref name="schemaName"/>, <paramref name="contentType"/> or <paramref name="schemaContent"/> is null. </exception>
        /// <remarks>
        /// Register new schema. If schema of specified name does not exist in specified group, schema is created at version 1. If schema of specified name exists already in specified group, schema is created at latest version + 1.
        ///
        /// </remarks>
        public async Task<ResponseWithHeaders<SchemaRegisterHeaders>> RegisterAsync(string groupName, string schemaName, string contentType, Stream schemaContent, CancellationToken cancellationToken = default)
        {
            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }
            if (schemaName == null)
            {
                throw new ArgumentNullException(nameof(schemaName));
            }
            if (contentType == null)
            {
                throw new ArgumentNullException(nameof(contentType));
            }
            if (schemaContent == null)
            {
                throw new ArgumentNullException(nameof(schemaContent));
            }

            using var message = CreateRegisterRequest(groupName, schemaName, contentType, schemaContent);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
            var headers = new SchemaRegisterHeaders(message.Response);
            switch (message.Response.Status)
            {
                case 204:
                    return ResponseWithHeaders.FromValue(headers, message.Response);
                default:
                    throw await ClientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false);
            }
        }

        /// <summary> Register new schema. </summary>
        /// <param name="groupName"> Schema group under which schema should be registered.  Group's serialization type should match the serialization type specified in the request. </param>
        /// <param name="schemaName"> Name of schema. </param>
        /// <param name="contentType"> Content type of the schema. </param>
        /// <param name="schemaContent"> String representation (UTF-8) of the schema being registered. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="groupName"/>, <paramref name="schemaName"/>, <paramref name="contentType"/> or <paramref name="schemaContent"/> is null. </exception>
        /// <remarks>
        /// Register new schema. If schema of specified name does not exist in specified group, schema is created at version 1. If schema of specified name exists already in specified group, schema is created at latest version + 1.
        ///
        /// </remarks>
        public ResponseWithHeaders<SchemaRegisterHeaders> Register(string groupName, string schemaName, string contentType, Stream schemaContent, CancellationToken cancellationToken = default)
        {
            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }
            if (schemaName == null)
            {
                throw new ArgumentNullException(nameof(schemaName));
            }
            if (contentType == null)
            {
                throw new ArgumentNullException(nameof(contentType));
            }
            if (schemaContent == null)
            {
                throw new ArgumentNullException(nameof(schemaContent));
            }

            using var message = CreateRegisterRequest(groupName, schemaName, contentType, schemaContent);
            _pipeline.Send(message, cancellationToken);
            var headers = new SchemaRegisterHeaders(message.Response);
            switch (message.Response.Status)
            {
                case 204:
                    return ResponseWithHeaders.FromValue(headers, message.Response);
                default:
                    throw ClientDiagnostics.CreateRequestFailedException(message.Response);
            }
        }
    }
}


text:
package org.apache.solr.cloud;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.lucene.util.LuceneTestCase.Slow;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.cloud.overseer.OverseerAction;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.CollectionParams;
import org.apache.solr.common.util.ExecutorUtil;
import org.apache.solr.common.util.Utils;
import org.apache.solr.core.CloudConfig;
import org.apache.solr.handler.component.HttpShardHandlerFactory;
import org.apache.solr.update.UpdateShardHandler;
import org.apache.solr.update.UpdateShardHandlerConfig;
import org.apache.solr.util.DefaultSolrThreadFactory;
import org.apache.solr.util.stats.Snapshot;
import org.apache.solr.util.stats.Timer;
import org.apache.solr.util.stats.TimerContext;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.NoNodeException;
import org.apache.zookeeper.KeeperException.NodeExistsException;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.xml.sax.SAXException;

import static org.apache.solr.cloud.AbstractDistribZkTestBase.verifyReplicaStatus;

@Slow
public class OverseerTest extends SolrTestCaseJ4 {

  static final int TIMEOUT = 30000;
  private static final boolean DEBUG = false;

  private List<Overseer> overseers = new ArrayList<>();
  private List<ZkStateReader> readers = new ArrayList<>();
  private List<HttpShardHandlerFactory> httpShardHandlerFactorys = new ArrayList<>();
  private List<UpdateShardHandler> updateShardHandlers = new ArrayList<>();

  private String collection = "collection1";

  public static class MockZKController{

    private final SolrZkClient zkClient;
    private final ZkStateReader zkStateReader;
    private final String nodeName;
    private final Map<String, ElectionContext> electionContext = Collections.synchronizedMap(new HashMap<String, ElectionContext>());

    public MockZKController(String zkAddress, String nodeName) throws InterruptedException, TimeoutException, IOException, KeeperException {
      this.nodeName = nodeName;
      zkClient = new SolrZkClient(zkAddress, TIMEOUT);

      ZkController.createClusterZkNodes(zkClient);

      zkStateReader = new ZkStateReader(zkClient);
      zkStateReader.createClusterStateWatchersAndUpdate();

      // live node
      final String nodePath = ZkStateReader.LIVE_NODES_ZKNODE + "/" + nodeName;
      zkClient.makePath(nodePath, CreateMode.EPHEMERAL, true);
    }

    private void deleteNode(final String path) {

      try {
        zkClient.delete(path, -1, true);
      } catch (NoNodeException e) {
        // fine
        log.warn("cancelElection did not find election node to remove");
      } catch (KeeperException e) {
        fail("Unexpected KeeperException!" + e);
      } catch (InterruptedException e) {
        fail("Unexpected InterruptedException!" + e);
      }
    }

    public void close() {
      deleteNode(ZkStateReader.LIVE_NODES_ZKNODE + "/" + nodeName);
      zkClient.close();
    }

    public String publishState(String collection, String coreName, String coreNodeName, Replica.State stateName, int numShards)
        throws KeeperException, InterruptedException, IOException {
      if (stateName == null) {
        ElectionContext ec = electionContext.remove(coreName);
        if (ec != null) {
          ec.cancelElection();
        }
        ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.DELETECORE.toLower(),
            ZkStateReader.NODE_NAME_PROP, nodeName,
            ZkStateReader.CORE_NAME_PROP, coreName,
            ZkStateReader.CORE_NODE_NAME_PROP, coreNodeName,
            ZkStateReader.COLLECTION_PROP, collection);
            DistributedQueue q = Overseer.getInQueue(zkClient);
            q.offer(Utils.toJSON(m));
         return null;
      } else {
        ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.STATE.toLower(),
        ZkStateReader.STATE_PROP, stateName.toString(),
        ZkStateReader.NODE_NAME_PROP, nodeName,
        ZkStateReader.CORE_NAME_PROP, coreName,
        ZkStateReader.CORE_NODE_NAME_PROP, coreNodeName,
        ZkStateReader.COLLECTION_PROP, collection,
        ZkStateReader.NUM_SHARDS_PROP, Integer.toString(numShards),
        ZkStateReader.BASE_URL_PROP, "http://" + nodeName + "/solr/");
        DistributedQueue q = Overseer.getInQueue(zkClient);
        q.offer(Utils.toJSON(m));
      }

      if (collection.length() > 0) {
        for (int i = 0; i < 120; i++) {
          String shardId = getShardId(collection, coreNodeName);
          if (shardId != null) {
            try {
              zkClient.makePath("/collections/" + collection + "/leader_elect/"
                  + shardId + "/election", true);
            } catch (NodeExistsException nee) {}
            ZkNodeProps props = new ZkNodeProps(ZkStateReader.BASE_URL_PROP,
                "http://" + nodeName + "/solr/", ZkStateReader.NODE_NAME_PROP,
                nodeName, ZkStateReader.CORE_NAME_PROP, coreName,
                ZkStateReader.SHARD_ID_PROP, shardId,
                ZkStateReader.COLLECTION_PROP, collection,
                ZkStateReader.CORE_NODE_NAME_PROP, coreNodeName);
            LeaderElector elector = new LeaderElector(zkClient);
            ShardLeaderElectionContextBase ctx = new ShardLeaderElectionContextBase(
                elector, shardId, collection, nodeName + "_" + coreName, props,
                zkStateReader);
            elector.setup(ctx);
            elector.joinElection(ctx, false);
            return shardId;
          }
          Thread.sleep(500);
        }
      }
      return null;
    }

    private String getShardId(String collection, String coreNodeName) {
      Map<String,Slice> slices = zkStateReader.getClusterState().getSlicesMap(collection);
      if (slices != null) {
        for (Slice slice : slices.values()) {
          for (Replica replica : slice.getReplicas()) {
            String cnn = replica.getName();
            if (coreNodeName.equals(cnn)) {
              return slice.getName();
            }
          }
        }
      }
      return null;
    }
  }

  @BeforeClass
  public static void beforeClass() throws Exception {
    initCore();
  }

  @AfterClass
  public static void afterClass() throws Exception {
    Thread.sleep(3000); //XXX wait for threads to die...
  }

  @After
  public void tearDown() throws Exception {
    super.tearDown();
    for (Overseer overseer : overseers) {
      overseer.close();
    }
    overseers.clear();
    for (ZkStateReader reader : readers) {
      reader.close();
    }
    readers.clear();

    for (HttpShardHandlerFactory handlerFactory : httpShardHandlerFactorys) {
      handlerFactory.close();
    }
    httpShardHandlerFactorys.clear();

    for (UpdateShardHandler updateShardHandler : updateShardHandlers) {
      updateShardHandler.close();
    }
    updateShardHandlers.clear();
  }

  @Test
  public void testShardAssignment() throws Exception {
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();

    ZkTestServer server = new ZkTestServer(zkDir);

    MockZKController zkController = null;
    SolrZkClient zkClient = null;
    SolrZkClient overseerClient = null;

    try {
      server.run();
      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());

      zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
      ZkController.createClusterZkNodes(zkClient);

      overseerClient = electNewOverseer(server.getZkAddress());

      ZkStateReader reader = new ZkStateReader(zkClient);
      reader.createClusterStateWatchersAndUpdate();

      zkController = new MockZKController(server.getZkAddress(), "127.0.0.1");

      final int numShards=6;

      for (int i = 0; i < numShards; i++) {
        assertNotNull("shard got no id?", zkController.publishState(collection, "core" + (i+1), "node" + (i+1), Replica.State.ACTIVE, 3));
      }
      Map<String,Replica> rmap = reader.getClusterState().getSlice("collection1", "shard1").getReplicasMap();
      assertEquals(rmap.toString(), 2, rmap.size());
      assertEquals(rmap.toString(), 2, reader.getClusterState().getSlice("collection1", "shard2").getReplicasMap().size());
      assertEquals(rmap.toString(), 2, reader.getClusterState().getSlice("collection1", "shard3").getReplicasMap().size());

      //make sure leaders are in cloud state
      assertNotNull(reader.getLeaderUrl("collection1", "shard1", 15000));
      assertNotNull(reader.getLeaderUrl("collection1", "shard2", 15000));
      assertNotNull(reader.getLeaderUrl("collection1", "shard3", 15000));

    } finally {
      if (DEBUG) {
        if (zkController != null) {
          zkClient.printLayoutToStdOut();
        }
      }
      close(zkClient);
      if (zkController != null) {
        zkController.close();
      }
      close(overseerClient);
      server.shutdown();
    }
  }

  @Test
  public void testBadQueueItem() throws Exception {
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();

    ZkTestServer server = new ZkTestServer(zkDir);

    MockZKController zkController = null;
    SolrZkClient zkClient = null;
    SolrZkClient overseerClient = null;

    try {
      server.run();
      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());

      zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
      ZkController.createClusterZkNodes(zkClient);

      overseerClient = electNewOverseer(server.getZkAddress());

      ZkStateReader reader = new ZkStateReader(zkClient);
      reader.createClusterStateWatchersAndUpdate();

      zkController = new MockZKController(server.getZkAddress(), "127.0.0.1");

      final int numShards=3;

      for (int i = 0; i < numShards; i++) {
        assertNotNull("shard got no id?", zkController.publishState(collection, "core" + (i+1), "node" + (i+1), Replica.State.ACTIVE, 3));
      }

      assertEquals(1, reader.getClusterState().getSlice(collection, "shard1").getReplicasMap().size());
      assertEquals(1, reader.getClusterState().getSlice(collection, "shard2").getReplicasMap().size());
      assertEquals(1, reader.getClusterState().getSlice(collection, "shard3").getReplicasMap().size());

      //make sure leaders are in cloud state
      assertNotNull(reader.getLeaderUrl(collection, "shard1", 15000));
      assertNotNull(reader.getLeaderUrl(collection, "shard2", 15000));
      assertNotNull(reader.getLeaderUrl(collection, "shard3", 15000));

      // publish a bad queue item
      String emptyCollectionName = "";
      zkController.publishState(emptyCollectionName, "core0", "node0", Replica.State.ACTIVE, 1);
      zkController.publishState(emptyCollectionName, "core0", "node0", null, 1);

      // make sure the Overseer is still processing items
      for (int i = 0; i < numShards; i++) {
        assertNotNull("shard got no id?", zkController.publishState("collection2", "core" + (i + 1), "node" + (i + 1), Replica.State.ACTIVE, 3));
      }

      assertEquals(1, reader.getClusterState().getSlice("collection2", "shard1").getReplicasMap().size());
      assertEquals(1, reader.getClusterState().getSlice("collection2", "shard2").getReplicasMap().size());
      assertEquals(1, reader.getClusterState().getSlice("collection2", "shard3").getReplicasMap().size());

      //make sure leaders are in cloud state
      assertNotNull(reader.getLeaderUrl("collection2", "shard1", 15000));
      assertNotNull(reader.getLeaderUrl("collection2", "shard2", 15000));
      assertNotNull(reader.getLeaderUrl("collection2", "shard3", 15000));

    } finally {
      if (DEBUG) {
        if (zkController != null) {
          zkClient.printLayoutToStdOut();
        }
      }
      close(zkClient);
      if (zkController != null) {
        zkController.close();
      }
      close(overseerClient);
      server.shutdown();
    }
  }

  @Test
  public void testShardAssignmentBigger() throws Exception {
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();

    final int nodeCount = random().nextInt(TEST_NIGHTLY ? 50 : 10)+(TEST_NIGHTLY ? 50 : 10)+1;   //how many simulated nodes (num of threads)
    final int coreCount = random().nextInt(TEST_NIGHTLY ? 100 : 11)+(TEST_NIGHTLY ? 100 : 11)+1; //how many cores to register
    final int sliceCount = random().nextInt(TEST_NIGHTLY ? 20 : 5)+1;  //how many slices

    ZkTestServer server = new ZkTestServer(zkDir);

    SolrZkClient zkClient = null;
    ZkStateReader reader = null;
    SolrZkClient overseerClient = null;

    final MockZKController[] controllers = new MockZKController[nodeCount];
    final ExecutorService[] nodeExecutors = new ExecutorService[nodeCount];
    try {
      server.run();
      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());

      zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
      ZkController.createClusterZkNodes(zkClient);

      overseerClient = electNewOverseer(server.getZkAddress());

      reader = new ZkStateReader(zkClient);
      reader.createClusterStateWatchersAndUpdate();

      for (int i = 0; i < nodeCount; i++) {
        controllers[i] = new MockZKController(server.getZkAddress(), "node" + i);
      }
      for (int i = 0; i < nodeCount; i++) {
        nodeExecutors[i] = ExecutorUtil.newMDCAwareFixedThreadPool(1, new DefaultSolrThreadFactory("testShardAssignment"));
      }

      final String[] ids = new String[coreCount];
      //register total of coreCount cores
      for (int i = 0; i < coreCount; i++) {
        final int slot = i;
        Runnable coreStarter = new Runnable() {
          @Override
          public void run() {

            final String coreName = "core" + slot;

            try {
              ids[slot]=controllers[slot % nodeCount].publishState(collection, coreName, "node" + slot, Replica.State.ACTIVE, sliceCount);
            } catch (Throwable e) {
              e.printStackTrace();
              fail("register threw exception:" + e.getClass());
            }
          }
        };

        nodeExecutors[i % nodeCount].submit(coreStarter);
      }

      for (int i = 0; i < nodeCount; i++) {
        nodeExecutors[i].shutdown();
      }

      for (int i = 0; i < nodeCount; i++) {
        while (!nodeExecutors[i].awaitTermination(100, TimeUnit.MILLISECONDS));
      }

      // make sure all cores have been assigned a id in cloudstate
      int cloudStateSliceCount = 0;
      for (int i = 0; i < 40; i++) {
        cloudStateSliceCount = 0;
        reader.updateClusterState();
        ClusterState state = reader.getClusterState();
        Map<String,Slice> slices = state.getSlicesMap("collection1");
        if (slices != null) {
          for (String name : slices.keySet()) {
            cloudStateSliceCount += slices.get(name).getReplicasMap().size();
          }
          if (coreCount == cloudStateSliceCount) break;
        }

        Thread.sleep(200);
      }
      assertEquals("Unable to verify all cores have been assigned an id in cloudstate",
                   coreCount, cloudStateSliceCount);

      // make sure all cores have been returned an id
      int assignedCount = 0;
      for (int i = 0; i < 240; i++) {
        assignedCount = 0;
        for (int j = 0; j < coreCount; j++) {
          if (ids[j] != null) {
            assignedCount++;
          }
        }
        if (coreCount == assignedCount) {
          break;
        }
        Thread.sleep(1000);
      }
      assertEquals("Unable to verify all cores have been returned an id",
                   coreCount, assignedCount);

      final HashMap<String, AtomicInteger> counters = new HashMap<>();
      for (int i = 1; i < sliceCount+1; i++) {
        counters.put("shard" + i, new AtomicInteger());
      }

      for (int i = 0; i < coreCount; i++) {
        final AtomicInteger ai = counters.get(ids[i]);
        assertNotNull("could not find counter for shard:" + ids[i], ai);
        ai.incrementAndGet();
      }

      for (String counter: counters.keySet()) {
        int count = counters.get(counter).intValue();
        int expectedCount = coreCount / sliceCount;
        int min = expectedCount - 1;
        int max = expectedCount + 1;
        if (count < min || count > max) {
          fail("Unevenly assigned shard ids, " + counter + " had " + count
              + ", expected: " + min + "-" + max);
        }
      }

      //make sure leaders are in cloud state
      for (int i = 0; i < sliceCount; i++) {
        assertNotNull(reader.getLeaderUrl("collection1", "shard" + (i + 1), 15000));
      }

    } finally {
      if (DEBUG) {
        if (controllers[0] != null) {
          zkClient.printLayoutToStdOut();
        }
      }
      close(zkClient);
      close(overseerClient);
      close(reader);
      for (int i = 0; i < controllers.length; i++)
        if (controllers[i] != null) {
          controllers[i].close();
        }
      server.shutdown();
      for (int i = 0; i < nodeCount; i++) {
        if (nodeExecutors[i] != null) {
          nodeExecutors[i].shutdownNow();
        }
      }
    }
  }

  //wait until collections are available
  private void waitForCollections(ZkStateReader stateReader, String... collections) throws InterruptedException, KeeperException {
    int maxIterations = 100;
    while (0 < maxIterations--) {
      stateReader.updateClusterState();
      final ClusterState state = stateReader.getClusterState();
      Set<String> availableCollections = state.getCollections();
      int availableCount = 0;
      for(String requiredCollection: collections) {
        if(availableCollections.contains(requiredCollection)) {
          availableCount++;
        }
        if(availableCount == collections.length) return;
        Thread.sleep(50);
      }
    }
    log.warn("Timeout waiting for collections: " + Arrays.asList(collections) + " state:" + stateReader.getClusterState());
  }

  @Test
  public void testStateChange() throws Exception {
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();

    ZkTestServer server = new ZkTestServer(zkDir);

    SolrZkClient zkClient = null;
    ZkStateReader reader = null;
    SolrZkClient overseerClient = null;

    try {
      server.run();
      zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);

      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
      ZkController.createClusterZkNodes(zkClient);

      reader = new ZkStateReader(zkClient);
      reader.createClusterStateWatchersAndUpdate();

      overseerClient = electNewOverseer(server.getZkAddress());

      DistributedQueue q = Overseer.getInQueue(zkClient);

      ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.STATE.toLower(),
          ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr",
          ZkStateReader.NODE_NAME_PROP, "node1",
          ZkStateReader.COLLECTION_PROP, "collection1",
          ZkStateReader.CORE_NAME_PROP, "core1",
          ZkStateReader.ROLES_PROP, "",
          ZkStateReader.STATE_PROP, Replica.State.RECOVERING.toString());

      q.offer(Utils.toJSON(m));

      waitForCollections(reader, "collection1");

      assertSame(reader.getClusterState().toString(), Replica.State.RECOVERING,
          reader.getClusterState().getSlice("collection1", "shard1").getReplica("core_node1").getState());

      //publish node state (active)
      m = new ZkNodeProps(Overseer.QUEUE_OPERATION, "state",
          ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr",
          ZkStateReader.NODE_NAME_PROP, "node1",
          ZkStateReader.COLLECTION_PROP, "collection1",
          ZkStateReader.CORE_NAME_PROP, "core1",
          ZkStateReader.ROLES_PROP, "",
          ZkStateReader.STATE_PROP, Replica.State.ACTIVE.toString());

      q.offer(Utils.toJSON(m));

      verifyReplicaStatus(reader, "collection1", "shard1", "core_node1", Replica.State.ACTIVE);

    } finally {

      close(zkClient);
      close(overseerClient);

      close(reader);
      server.shutdown();
    }
  }

  private void verifyShardLeader(ZkStateReader reader, String collection, String shard, String expectedCore) throws InterruptedException, KeeperException {
    int maxIterations = 200;
    while(maxIterations-->0) {
      reader.updateClusterState(); // poll state
      ZkNodeProps props =  reader.getClusterState().getLeader(collection, shard);
      if(props!=null) {
        if(expectedCore.equals(props.getStr(ZkStateReader.CORE_NAME_PROP))) {
          return;
        }
      }
      Thread.sleep(200);
    }

    assertEquals("Unexpected shard leader coll:" + collection + " shard:" + shard, expectedCore, (reader.getClusterState().getLeader(collection, shard)!=null)?reader.getClusterState().getLeader(collection, shard).getStr(ZkStateReader.CORE_NAME_PROP):null);
  }

  @Test
  public void testOverseerFailure() throws Exception {
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();
    ZkTestServer server = new ZkTestServer(zkDir);

    SolrZkClient overseerClient = null;
    ZkStateReader reader = null;
    MockZKController mockController = null;

    SolrZkClient zkClient = null;
    try {
      server.run();

      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());

      zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);

      ZkController.createClusterZkNodes(zkClient);

      reader = new ZkStateReader(zkClient);
      reader.createClusterStateWatchersAndUpdate();

      mockController = new MockZKController(server.getZkAddress(), "node1");

      overseerClient = electNewOverseer(server.getZkAddress());

      Thread.sleep(1000);
      mockController.publishState(collection, "core1", "core_node1", Replica.State.RECOVERING, 1);

      waitForCollections(reader, collection);
      verifyReplicaStatus(reader, collection, "shard1", "core_node1", Replica.State.RECOVERING);

      int version = getClusterStateVersion(zkClient);

      mockController.publishState(collection, "core1", "core_node1", Replica.State.ACTIVE, 1);

      while (version == getClusterStateVersion(zkClient));

      verifyReplicaStatus(reader, collection, "shard1", "core_node1", Replica.State.ACTIVE);
      version = getClusterStateVersion(zkClient);
      overseerClient.close();
      Thread.sleep(1000); // wait for overseer to get killed

      mockController.publishState(collection, "core1", "core_node1", Replica.State.RECOVERING, 1);
      version = getClusterStateVersion(zkClient);

      overseerClient = electNewOverseer(server.getZkAddress());

      while (version == getClusterStateVersion(zkClient));

      verifyReplicaStatus(reader, collection, "shard1", "core_node1", Replica.State.RECOVERING);

      assertEquals("Live nodes count does not match", 1, reader
          .getClusterState().getLiveNodes().size());
      assertEquals("Shard count does not match", 1, reader.getClusterState()
          .getSlice(collection, "shard1").getReplicasMap().size());
      version = getClusterStateVersion(zkClient);
      mockController.publishState(collection, "core1", "core_node1", null, 1);
      while (version == getClusterStateVersion(zkClient));
      Thread.sleep(500);
      assertFalse("collection1 should be gone after publishing the null state",
          reader.getClusterState().hasCollection(collection));
    } finally {
      close(mockController);
      close(overseerClient);
      close(zkClient);
      close(reader);
      server.shutdown();
    }
  }

  @Test
  public void testOverseerStatsReset() throws Exception {
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();
    ZkTestServer server = new ZkTestServer(zkDir);
    ZkStateReader reader = null;
    MockZKController mockController = null;

    SolrZkClient zkClient = null;
    try {
      server.run();

      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());

      zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);

      ZkController.createClusterZkNodes(zkClient);

      reader = new ZkStateReader(zkClient);
      reader.createClusterStateWatchersAndUpdate();

      mockController = new MockZKController(server.getZkAddress(), "node1");

      LeaderElector overseerElector = new LeaderElector(zkClient);
      if (overseers.size() > 0) {
        overseers.get(overseers.size() -1).close();
        overseers.get(overseers.size() -1).getZkStateReader().getZkClient().close();
      }
      UpdateShardHandler updateShardHandler = new UpdateShardHandler(UpdateShardHandlerConfig.DEFAULT);
      updateShardHandlers.add(updateShardHandler);
      HttpShardHandlerFactory httpShardHandlerFactory = new HttpShardHandlerFactory();
      httpShardHandlerFactorys.add(httpShardHandlerFactory);
      Overseer overseer = new Overseer(httpShardHandlerFactory.getShardHandler(), updateShardHandler, "/admin/cores", reader, null,
          new CloudConfig.CloudConfigBuilder("127.0.0.1", 8983, "").build());
      overseers.add(overseer);
      ElectionContext ec = new OverseerElectionContext(zkClient, overseer,
          server.getZkAddress().replaceAll("/", "_"));
      overseerElector.setup(ec);
      overseerElector.joinElection(ec, false);

      mockController.publishState(collection, "core1", "core_node1", Replica.State.ACTIVE, 1);

      assertNotNull(overseer.getStats());
      assertTrue((overseer.getStats().getSuccessCount(OverseerAction.STATE.toLower())) > 0);

      // shut it down
      overseer.close();
      ec.cancelElection();

      // start it again
      overseerElector.setup(ec);
      overseerElector.joinElection(ec, false);
      assertNotNull(overseer.getStats());
      assertEquals(0, (overseer.getStats().getSuccessCount(OverseerAction.STATE.toLower())));

    } finally {
      close(mockController);
      close(zkClient);
      close(reader);
      server.shutdown();
    }
  }

  private AtomicInteger killCounter = new AtomicInteger();

  private class OverseerRestarter implements Runnable{
    SolrZkClient overseerClient = null;
    public volatile boolean run = true;
    private final String zkAddress;

    public OverseerRestarter(String zkAddress) {
      this.zkAddress = zkAddress;
    }

    @Override
    public void run() {
      try {
        overseerClient = electNewOverseer(zkAddress);
        while (run) {
          if (killCounter.get()>0) {
            try {
              killCounter.decrementAndGet();
              log.info("Killing overseer.");
              overseerClient.close();
              overseerClient = electNewOverseer(zkAddress);
            } catch (Throwable e) {
              // e.printStackTrace();
            }
          }
          try {
            Thread.sleep(100);
          } catch (Throwable e) {
            // e.printStackTrace();
          }
        }
      } catch (Throwable t) {
        // ignore
      } finally {
        if (overseerClient != null) {
          try {
            overseerClient.close();
          } catch (Throwable t) {
            // ignore
          }
        }
      }
    }
  }

  @Test
  public void testShardLeaderChange() throws Exception {
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();
    final ZkTestServer server = new ZkTestServer(zkDir);
    SolrZkClient controllerClient = null;
    ZkStateReader reader = null;
    MockZKController mockController = null;
    MockZKController mockController2 = null;
    OverseerRestarter killer = null;
    Thread killerThread = null;
    try {
      server.run();
      controllerClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
      ZkController.createClusterZkNodes(controllerClient);

      killer = new OverseerRestarter(server.getZkAddress());
      killerThread = new Thread(killer);
      killerThread.start();

      reader = new ZkStateReader(controllerClient); //no watches, we'll poll

      for (int i = 0; i < atLeast(4); i++) {
        killCounter.incrementAndGet(); //for each round allow 1 kill
        mockController = new MockZKController(server.getZkAddress(), "node1");
        mockController.publishState(collection, "core1", "node1", Replica.State.ACTIVE,1);
        if(mockController2!=null) {
          mockController2.close();
          mockController2 = null;
        }
        mockController.publishState(collection, "core1", "node1",Replica.State.RECOVERING,1);
        mockController2 = new MockZKController(server.getZkAddress(), "node2");
        mockController.publishState(collection, "core1", "node1", Replica.State.ACTIVE,1);
        verifyShardLeader(reader, "collection1", "shard1", "core1");
        mockController2.publishState(collection, "core4", "node2", Replica.State.ACTIVE ,1);
        mockController.close();
        mockController = null;
        verifyShardLeader(reader, "collection1", "shard1", "core4");
      }
    } finally {
      if (killer != null) {
        killer.run = false;
        if (killerThread != null) {
          killerThread.join();
        }
      }
      close(mockController);
      close(mockController2);
      close(controllerClient);
      close(reader);
      server.shutdown();
    }
  }

  @Test
  public void testDoubleAssignment() throws Exception {
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();

    ZkTestServer server = new ZkTestServer(zkDir);

    SolrZkClient controllerClient = null;
    SolrZkClient overseerClient = null;
    ZkStateReader reader = null;
    MockZKController mockController = null;

    try {
      server.run();
      controllerClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);

      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
      ZkController.createClusterZkNodes(controllerClient);

      reader = new ZkStateReader(controllerClient);
      reader.createClusterStateWatchersAndUpdate();

      mockController = new MockZKController(server.getZkAddress(), "node1");

      overseerClient = electNewOverseer(server.getZkAddress());

      mockController.publishState(collection, "core1", "core_node1", Replica.State.RECOVERING, 1);

      waitForCollections(reader, "collection1");

      verifyReplicaStatus(reader, collection, "shard1", "core_node1", Replica.State.RECOVERING);

      mockController.close();

      int version = getClusterStateVersion(controllerClient);

      mockController = new MockZKController(server.getZkAddress(), "node1");
      mockController.publishState(collection, "core1", "core_node1", Replica.State.RECOVERING, 1);

      while (version == getClusterStateVersion(controllerClient));

      reader.updateClusterState();
      ClusterState state = reader.getClusterState();

      int numFound = 0;
      for (String  c : state.getCollections()) {
        DocCollection collection = state.getCollection(c);
        for (Slice slice : collection.getSlices()) {
          if (slice.getReplicasMap().get("core_node1") != null) {
            numFound++;
          }
        }
      }
      assertEquals("Shard was found more than once in ClusterState", 1,
          numFound);
    } finally {
      close(overseerClient);
      close(mockController);
      close(controllerClient);
      close(reader);
      server.shutdown();
    }
  }

  @Test
  public void testPlaceholders() throws Exception {
    String zkDir = createTempDir("zkData").toFile().getAbsolutePath();

    ZkTestServer server = new ZkTestServer(zkDir);

    SolrZkClient controllerClient = null;
    SolrZkClient overseerClient = null;
    ZkStateReader reader = null;
    MockZKController mockController = null;

    try {
      server.run();
      controllerClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);

      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
      ZkController.createClusterZkNodes(controllerClient);

      reader = new ZkStateReader(controllerClient);
      reader.createClusterStateWatchersAndUpdate();

      mockController = new MockZKController(server.getZkAddress(), "node1");

      overseerClient = electNewOverseer(server.getZkAddress());

      mockController.publishState(collection, "core1", "node1", Replica.State.RECOVERING, 12);

      waitForCollections(reader, "collection1");

      assertEquals("Slicecount does not match", 12, reader.getClusterState().getSlices("collection1").size());

    } finally {
      close(overseerClient);
      close(mockController);
      close(controllerClient);
      close(reader);
      server.shutdown();
    }
  }

  @Test
  @Ignore
  public void testPerformance() throws Exception {
    String zkDir = createTempDir("OverseerTest.testPerformance").toFile().getAbsolutePath();

    ZkTestServer server = new ZkTestServer(zkDir);

    SolrZkClient controllerClient = null;
    SolrZkClient overseerClient = null;
    ZkStateReader reader = null;
    MockZKController mockController = null;

    try {
      server.run();
      controllerClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);

      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
      ZkController.createClusterZkNodes(controllerClient);

      reader = new ZkStateReader(controllerClient);
      reader.createClusterStateWatchersAndUpdate();

      mockController = new MockZKController(server.getZkAddress(), "node1");

      final int MAX_COLLECTIONS = 10, MAX_CORES = 10, MAX_STATE_CHANGES = 20000, STATE_FORMAT = 2;

      for (int i=0; i<MAX_COLLECTIONS; i++)  {
        ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, CollectionParams.CollectionAction.CREATE.toLower(),
            "name", "perf" + i,
            ZkStateReader.NUM_SHARDS_PROP, "1",
            "stateFormat", String.valueOf(STATE_FORMAT),
            ZkStateReader.REPLICATION_FACTOR, "1",
            ZkStateReader.MAX_SHARDS_PER_NODE, "1"
            );
        DistributedQueue q = Overseer.getInQueue(controllerClient);
        q.offer(Utils.toJSON(m));
        controllerClient.makePath("/collections/perf" + i, true);
      }

      for (int i = 0, j = 0, k = 0; i < MAX_STATE_CHANGES; i++, j++, k++) {
        ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.STATE.toLower(),
            ZkStateReader.STATE_PROP, Replica.State.RECOVERING.toString(),
            ZkStateReader.NODE_NAME_PROP,  "node1",
            ZkStateReader.CORE_NAME_PROP, "core" + k,
            ZkStateReader.CORE_NODE_NAME_PROP, "node1",
            ZkStateReader.COLLECTION_PROP, "perf" + j,
            ZkStateReader.NUM_SHARDS_PROP, "1",
            ZkStateReader.BASE_URL_PROP, "http://" +  "node1"
            + "/solr/");
        DistributedQueue q = Overseer.getInQueue(controllerClient);
        q.offer(Utils.toJSON(m));
        if (j >= MAX_COLLECTIONS - 1) j = 0;
        if (k >= MAX_CORES - 1) k = 0;
        if (i > 0 && i % 100 == 0) log.info("Published {} items", i);
      }

      // let's publish a sentinel collection which we'll use to wait for overseer to complete operations
      ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.STATE.toLower(),
          ZkStateReader.STATE_PROP, Replica.State.ACTIVE.toString(),
          ZkStateReader.NODE_NAME_PROP, "node1",
          ZkStateReader.CORE_NAME_PROP, "core1",
          ZkStateReader.CORE_NODE_NAME_PROP, "node1",
          ZkStateReader.COLLECTION_PROP, "perf_sentinel",
          ZkStateReader.NUM_SHARDS_PROP, "1",
          ZkStateReader.BASE_URL_PROP, "http://" + "node1"
          + "/solr/");
      DistributedQueue q = Overseer.getInQueue(controllerClient);
      q.offer(Utils.toJSON(m));

      Timer t = new Timer();
      TimerContext context = t.time();
      try {
        overseerClient = electNewOverseer(server.getZkAddress());
        assertTrue(overseers.size() > 0);

        while (true)  {
          reader.updateClusterState();
          ClusterState state = reader.getClusterState();
          if (state.hasCollection("perf_sentinel")) {
            break;
          }
          Thread.sleep(1000);
        }
      } finally {
        context.stop();
      }

      log.info("Overseer loop finished processing: ");
      printTimingStats(t);

      Overseer overseer = overseers.get(0);
      Overseer.Stats stats = overseer.getStats();

      String[] interestingOps = {"state", "update_state", "am_i_leader", ""};
      Arrays.sort(interestingOps);
      for (Map.Entry<String, Overseer.Stat> entry : stats.getStats().entrySet()) {
        String op = entry.getKey();
        if (Arrays.binarySearch(interestingOps, op) < 0)
          continue;
        Overseer.Stat stat = entry.getValue();
        log.info("op: {}, success: {}, failure: {}", op, stat.success.get(), stat.errors.get());
        Timer timer = stat.requestTime;
        printTimingStats(timer);
      }

    } finally {
      close(overseerClient);
      close(mockController);
      close(controllerClient);
      close(reader);
      server.shutdown();
    }
  }

  private void printTimingStats(Timer timer) {
    Snapshot snapshot = timer.getSnapshot();
    log.info("\t totalTime: {}", timer.getSum());
    log.info("\t avgRequestsPerMinute: {}", timer.getMeanRate());
    log.info("\t 5minRateRequestsPerMinute: {}", timer.getFiveMinuteRate());
    log.info("\t 15minRateRequestsPerMinute: {}", timer.getFifteenMinuteRate());
    log.info("\t avgTimePerRequest: {}", timer.getMean());
    log.info("\t medianRequestTime: {}", snapshot.getMedian());
    log.info("\t 75thPctlRequestTime: {}", snapshot.get75thPercentile());
    log.info("\t 95thPctlRequestTime: {}", snapshot.get95thPercentile());
    log.info("\t 99thPctlRequestTime: {}", snapshot.get99thPercentile());
    log.info("\t 999thPctlRequestTime: {}", snapshot.get999thPercentile());
  }

  private void close(MockZKController mockController) {
    if (mockController != null) {
      mockController.close();
    }
  }

  @Test
  public void testReplay() throws Exception{
    String zkDir = createTempDir().toFile().getAbsolutePath() + File.separator
        + "zookeeper/server1/data";
    ZkTestServer server = new ZkTestServer(zkDir);
    SolrZkClient zkClient = null;
    SolrZkClient overseerClient = null;
    ZkStateReader reader = null;

    try {
      server.run();
      zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
      ZkController.createClusterZkNodes(zkClient);

      reader = new ZkStateReader(zkClient);
      reader.createClusterStateWatchersAndUpdate();
      //prepopulate work queue with some items to emulate previous overseer died before persisting state
      DistributedQueue queue = Overseer.getInternalQueue(zkClient, new Overseer.Stats());
      ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, "state",
          ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr",
          ZkStateReader.NODE_NAME_PROP, "node1",
          ZkStateReader.SHARD_ID_PROP, "s1",
          ZkStateReader.COLLECTION_PROP, "collection1",
          ZkStateReader.CORE_NAME_PROP, "core1",
          ZkStateReader.ROLES_PROP, "",
          ZkStateReader.STATE_PROP, Replica.State.RECOVERING.toString());
      queue.offer(Utils.toJSON(m));
      m = new ZkNodeProps(Overseer.QUEUE_OPERATION, "state",
          ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr",
          ZkStateReader.NODE_NAME_PROP, "node1",
          ZkStateReader.SHARD_ID_PROP, "s1",
          ZkStateReader.COLLECTION_PROP, "collection1",
          ZkStateReader.CORE_NAME_PROP, "core2",
          ZkStateReader.ROLES_PROP, "",
          ZkStateReader.STATE_PROP, Replica.State.RECOVERING.toString());
      queue.offer(Utils.toJSON(m));

      overseerClient = electNewOverseer(server.getZkAddress());

      //submit to proper queue
      queue = Overseer.getInQueue(zkClient);
      m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.STATE.toLower(),
          ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr",
          ZkStateReader.NODE_NAME_PROP, "node1",
          ZkStateReader.SHARD_ID_PROP, "s1",
          ZkStateReader.COLLECTION_PROP, "collection1",
          ZkStateReader.CORE_NAME_PROP, "core3",
          ZkStateReader.ROLES_PROP, "",
          ZkStateReader.STATE_PROP, Replica.State.RECOVERING.toString());
      queue.offer(Utils.toJSON(m));

      for(int i=0;i<100;i++) {
        Slice s = reader.getClusterState().getSlice("collection1", "s1");
        if(s!=null && s.getReplicasMap().size()==3) break;
        Thread.sleep(100);
      }
      assertNotNull(reader.getClusterState().getSlice("collection1", "s1"));
      assertEquals(3, reader.getClusterState().getSlice("collection1", "s1").getReplicasMap().size());
    } finally {
      close(overseerClient);
      close(zkClient);
      close(reader);
      server.shutdown();
    }
  }

  @Test
  public void testExternalClusterStateChangeBehavior() throws Exception {
    String zkDir = createTempDir("testExternalClusterStateChangeBehavior").toFile().getAbsolutePath();

    ZkTestServer server = new ZkTestServer(zkDir);

    SolrZkClient zkClient = null;
    ZkStateReader reader = null;
    SolrZkClient overseerClient = null;

    try {
      server.run();
      zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);

      AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
      AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
      ZkController.createClusterZkNodes(zkClient);

      zkClient.create("/collections/test", null, CreateMode.PERSISTENT, true);

      reader = new ZkStateReader(zkClient);
      reader.createClusterStateWatchersAndUpdate();

      overseerClient = electNewOverseer(server.getZkAddress());

      DistributedQueue q = Overseer.getInQueue(zkClient);

      ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.STATE.toLower(),
          ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr",
          ZkStateReader.NODE_NAME_PROP, "node1",
          ZkStateReader.COLLECTION_PROP, "c1",
          ZkStateReader.CORE_NAME_PROP, "core1",
          ZkStateReader.ROLES_PROP, "",
          ZkStateReader.STATE_PROP, Replica.State.DOWN.toString());

      q.offer(Utils.toJSON(m));

      waitForCollections(reader, "c1");
      verifyReplicaStatus(reader, "c1", "shard1", "core_node1", Replica.State.DOWN);

      m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.STATE.toLower(),
          ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr",
          ZkStateReader.NODE_NAME_PROP, "node1",
          ZkStateReader.COLLECTION_PROP, "c1",
          ZkStateReader.CORE_NAME_PROP, "core1",
          ZkStateReader.ROLES_PROP, "",
          ZkStateReader.STATE_PROP, Replica.State.RECOVERING.toString());

      q.offer(Utils.toJSON(m));

      m = new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.STATE.toLower(),
          ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr",
          ZkStateReader.NODE_NAME_PROP, "node1",
          ZkStateReader.COLLECTION_PROP, "c1",
          ZkStateReader.CORE_NAME_PROP, "core1",
          ZkStateReader.ROLES_PROP, "",
          ZkStateReader.STATE_PROP, Replica.State.ACTIVE.toString());

      q.offer(Utils.toJSON(m));

      Stat stat = new Stat();
      byte[] data = zkClient.getData("/clusterstate.json", null, stat, true);
      // Simulate an external modification
      zkClient.setData("/clusterstate.json", data, true);

      m = new ZkNodeProps(Overseer.QUEUE_OPERATION, CollectionParams.CollectionAction.CREATE.toLower(),
          "name", "test",
          ZkStateReader.NUM_SHARDS_PROP, "1",
          ZkStateReader.REPLICATION_FACTOR, "1",
          DocCollection.STATE_FORMAT, "2"
      );
      q.offer(Utils.toJSON(m));

      m = new ZkNodeProps(Overseer.QUEUE_OPERATION, CollectionParams.CollectionAction.CREATESHARD.toLower(),
          "collection", "test",
          ZkStateReader.SHARD_ID_PROP, "x",
          ZkStateReader.REPLICATION_FACTOR, "1"
      );
      q.offer(Utils.toJSON(m));

      m = new ZkNodeProps(Overseer.QUEUE_OPERATION, CollectionParams.CollectionAction.ADDREPLICA.toLower(),
          "collection", "test",
          ZkStateReader.SHARD_ID_PROP, "x",
          ZkStateReader.BASE_URL_PROP, "http://127.0.0.1/solr",
          ZkStateReader.NODE_NAME_PROP, "node1",
          ZkStateReader.CORE_NAME_PROP, "core1",
          ZkStateReader.STATE_PROP, Replica.State.DOWN.toString()
      );
      q.offer(Utils.toJSON(m));

      waitForCollections(reader, "test");
      verifyReplicaStatus(reader, "test", "x", "core_node1", Replica.State.DOWN);

      waitForCollections(reader, "c1");
      verifyReplicaStatus(reader, "c1", "shard1", "core_node1", Replica.State.ACTIVE);

    } finally {
      close(zkClient);
      close(overseerClient);
      close(reader);
      server.shutdown();
    }
  }

  private void close(ZkStateReader reader) {
    if (reader != null) {
      reader.close();
    }
  }

  private void close(SolrZkClient client) throws InterruptedException {
    if (client != null) {
      client.close();
    }
  }

  private int getClusterStateVersion(SolrZkClient controllerClient)
      throws KeeperException, InterruptedException {
    return controllerClient.exists(ZkStateReader.CLUSTER_STATE, null, false).getVersion();
  }

  private SolrZkClient electNewOverseer(String address)
      throws InterruptedException, TimeoutException, IOException,
      KeeperException, ParserConfigurationException, SAXException {
    SolrZkClient zkClient = new SolrZkClient(address, TIMEOUT);
    ZkStateReader reader = new ZkStateReader(zkClient);
    readers.add(reader);
    LeaderElector overseerElector = new LeaderElector(zkClient);
    if (overseers.size() > 0) {
      overseers.get(overseers.size() -1).close();
      overseers.get(overseers.size() -1).getZkStateReader().getZkClient().close();
    }
    UpdateShardHandler updateShardHandler = new UpdateShardHandler(UpdateShardHandlerConfig.DEFAULT);
    updateShardHandlers.add(updateShardHandler);
    HttpShardHandlerFactory httpShardHandlerFactory = new HttpShardHandlerFactory();
    httpShardHandlerFactorys.add(httpShardHandlerFactory);
    Overseer overseer = new Overseer(httpShardHandlerFactory.getShardHandler(), updateShardHandler, "/admin/cores", reader, null,
        new CloudConfig.CloudConfigBuilder("127.0.0.1", 8983, "").build());
    overseers.add(overseer);
    ElectionContext ec = new OverseerElectionContext(zkClient, overseer,
        address.replaceAll("/", "_"));
    overseerElector.setup(ec);
    overseerElector.joinElection(ec, false);
    return zkClient;
  }

}


text:
<?xml version="1.0" encoding="UTF-8"?>
<class xmlns="http://xml.phpdox.net/src" full="StandardClass" namespace="" name="StandardClass" abstract="false" final="false" start="3" end="97">
  <file path="/var/www/tests/Project/_assets/project/src" file="StandardClass.php" realpath="/var/www/tests/Project/_assets/project/src/StandardClass.php" size="1700" time="2015-06-12T08:16:37+00:00" unixtime="1434096997" sha1="598a6532d89d5d3b9fba8b741019f978864f9698" relative="StandardClass.php"/>
  <constant type="boolean" name="CONSTANTS_NEVER_CHANGE" value="false">
    <docblock>
      <description/>
      <var type="boolean"/>
    </docblock>
  </constant>
  <constant type="integer" name="CHANGES_ARE_CONSTANTS" value="1">
    <docblock>
      <description/>
      <var type="integer"/>
    </docblock>
  </constant>
  <constant type="string" name="TYPE_NOT_DEFINED" value="'maybe'"/>
  <member name="publicMember" visibility="public" static="false" line="17"/>
  <member name="protectedMember" visibility="protected" static="false" line="19"/>
  <member name="privateMember" visibility="private" static="false" line="21"/>
  <member name="nonStaticMember" visibility="public" static="false" line="23"/>
  <member name="staticMember" visibility="public" static="true" line="25"/>
  <member name="objectsMember" visibility="public" static="false" line="30">
    <docblock>
      <description/>
      <var type="array" of="object">
        <type full="AbstractClass" namespace="" name="AbstractClass"/>
      </var>
    </docblock>
  </member>
  <member name="objectMember" visibility="public" static="false" line="35">
    <docblock>
      <description/>
      <var type="object">
        <type full="AbstractClass" namespace="" name="AbstractClass"/>
      </var>
    </docblock>
  </member>
  <member name="stringsMember" visibility="public" static="false" line="40">
    <docblock>
      <description/>
      <var type="array" of="string"/>
    </docblock>
  </member>
  <member name="stringMember" visibility="public" static="false" line="45">
    <docblock>
      <description/>
      <var type="string"/>
    </docblock>
  </member>
  <method name="publicMethod" start="47" end="47" abstract="false" final="false" static="false" visibility="public"/>
  <method name="protectedMethod" start="49" end="49" abstract="false" final="false" static="false" visibility="protected"/>
  <method name="privateMethod" start="51" end="51" abstract="false" final="false" static="false" visibility="private"/>
  <method name="nonStaticMethod" start="53" end="53" abstract="false" final="false" static="false" visibility="public"/>
  <method name="staticMethod" start="55" end="55" abstract="false" final="false" static="true" visibility="public"/>
  <method name="nonFinalMethod" start="57" end="57" abstract="false" final="false" static="false" visibility="public"/>
  <method name="finalMethod" start="59" end="59" abstract="false" final="true" static="false" visibility="public"/>
  <method name="nonAbstractMethod" start="61" end="61" abstract="false" final="false" static="false" visibility="public"/>
  <method name="abstractMethod" start="63" end="63" abstract="true" final="false" static="false" visibility="public"/>
  <method name="methodWithParameters" start="71" end="76" abstract="false" final="false" static="false" visibility="public">
    <docblock>
      <description/>
      <param type="object" variable="$object">
        <type full="AbstractClass" namespace="" name="AbstractClass"/>
      </param>
      <param type="array" variable="$objects" of="object">
        <type full="AbstractClass" namespace="" name="AbstractClass"/>
      </param>
      <param type="string" variable="$optional"/>
      <param type="array" variable="$either" of="string"/>
    </docblock>
    <parameter name="object" byreference="false" type="object">
      <type full="AbstractClass" namespace="" name="AbstractClass"/>
    </parameter>
    <parameter name="objects" byreference="false" type="array"/>
    <parameter name="optional" byreference="false" type="{unknown}" default="" constant="null"/>
    <parameter name="either" byreference="false" type="{unknown}" default="" constant="null"/>
  </method>
  <method name="returnStringMethod" start="81" end="81" abstract="false" final="false" static="false" visibility="public">
    <docblock>
      <description/>
      <return type="string"/>
    </docblock>
  </method>
  <method name="returnStringsMethod" start="86" end="86" abstract="false" final="false" static="false" visibility="public">
    <docblock>
      <description/>
      <return type="array" of="string"/>
    </docblock>
  </method>
  <method name="returnObjectMethod" start="91" end="91" abstract="false" final="false" static="false" visibility="public">
    <docblock>
      <description/>
      <return type="object">
        <type full="AbstractClass" namespace="" name="AbstractClass"/>
      </return>
    </docblock>
  </method>
  <method name="returnObjectsMethod" start="96" end="96" abstract="false" final="false" static="false" visibility="public">
    <docblock>
      <description/>
      <return type="array" of="object">
        <type full="AbstractClass" namespace="" name="AbstractClass"/>
      </return>
    </docblock>
  </method>
</class>


text:
SYNONYM

#### According to
The Catalogue of Life, 3rd January 2011

#### Published in
Bull. Soc. mycol. Fr. 8(2): 134 (1892)

#### Original name
Phyllachora philodendri Pat., 1892

### Remarks
null


text:
from qtpy import QtWidgets
from ..helpers import update_combobox

def test_update_combobox():
    combo = QtWidgets.QComboBox()
    update_combobox(combo, [('a', 1), ('b', 2)])
    update_combobox(combo, [('c', 3)])

def test_update_combobox_indexchanged():

    # Regression test for bug that caused currentIndexChanged to not be
    # emitted if the new index happened to be the same as the old one but the
    # label data was different.

    class MyComboBox(QtWidgets.QComboBox):

        def __init__(self, *args, **kwargs):
            self.change_count = 0
            super(MyComboBox, self).__init__(*args, **kwargs)
            self.currentIndexChanged.connect(self.changed)

        def changed(self):
            self.change_count += 1

    combo = MyComboBox()
    update_combobox(combo, [('a', 1), ('b', 2)])
    update_combobox(combo, [('c', 3)])

    assert combo.change_count == 2
    assert combo.currentIndex() == 0

    combo = MyComboBox()
    update_combobox(combo, [('a', 1), ('b', 2)])
    update_combobox(combo, [('a', 1), ('b', 3)])
    update_combobox(combo, [('a', 3), ('b', 1)])

    assert combo.change_count == 3
    assert combo.currentIndex() == 1


text:
setup [prometheus](http://prometheus.io/)

## Usage

All you need to do is to include the role and customize your prometheus configuration file:

``` yml
---
- hosts: vm
  remote_user: vagrant
  sudo: true
  roles:
    - nicolai86.prometheus
```

This will setup the latest version of prometheus as a system service and start it as well.

See the example directory on how to customize the configuration.


text:
package com.thaleslima.android.popularmovies.ui.detail;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.thaleslima.android.popularmovies.R;
import com.thaleslima.android.popularmovies.domain.Movie;
import com.thaleslima.android.popularmovies.ui.detail.fragment.MovieDetailFragment;
import com.thaleslima.android.popularmovies.utilities.ResolveUrlUtil;

public class MovieDetailActivity extends AppCompatActivity implements MovieDetailContract.View {
    private static final String EXTRA_MOVIE_DATA = "movie-data";

    private FloatingActionButton mFloatingActionButton;
    private ImageView mBackdropView;
    private FrameLayout mContainer;

    private MovieDetailContract.Presenter mPresenter;

    public static void navigate(Activity activity, Movie movie) {
        Intent intent = new Intent(activity, MovieDetailActivity.class);
        intent.putExtra(EXTRA_MOVIE_DATA, movie);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        initToolbar();
        setFindViewById();
        initFabFavorite();
        initialize();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPresenter.onPause();
    }

    private void initToolbar() {
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    private void setFindViewById() {
        mBackdropView = (ImageView) findViewById(R.id.iv_backdrop_movie);
        mContainer = (FrameLayout) findViewById(R.id.container);
    }

    private void initialize() {
        mPresenter = new MovieDetailPresenter(this);
        mPresenter.loadMovie();
    }

    @Override
    public Movie getMovieExtra() {
        Intent intent = getIntent();
        if (intent.hasExtra(EXTRA_MOVIE_DATA)) {
            return getIntent().getParcelableExtra(EXTRA_MOVIE_DATA);
        }

        return null;
    }

    @Override
    public void showMovie(Movie movie) {
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            setTitle(movie.originalTitle);
        }

        Picasso.with(this)
                .load(ResolveUrlUtil.getCompletePosterUrl(movie.backdropPath))
                .into(mBackdropView);

        initFragment(MovieDetailFragment.newInstance(movie));
    }

    @Override
    public Context getContext() {
        return this;
    }

    private void initFabFavorite() {
        mFloatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
        mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.saveOrRemoveFavorite();
            }
        });
    }

    @Override
    public void setIcoFavoriteYes() {
        mFloatingActionButton.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_favorite_white_24dp));
    }

    @Override
    public void showSnackbarRemoveFavorite() {
        Snackbar.make(mContainer, R.string.title_remove_favorite, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void showSnackbarSaveFavorite() {
        Snackbar.make(mContainer, R.string.title_save_favorite, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void setIcoFavoriteNo() {
        mFloatingActionButton.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_favorite_border_white_24dp));
    }

    private void initFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.container, fragment);
        transaction.commit();
    }
}