<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.handler.component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.cloud.AbstractDistribZkTestBase;
import org.apache.solr.cloud.SolrCloudTestCase;
import org.apache.solr.common.util.NamedList;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class UpdateLogCloudTest extends SolrCloudTestCase {

  private static String COLLECTION;
  private static final int NUM_SHARDS = 1;
  private static final int NUM_REPLICAS = 4;

  @BeforeClass
  public static void setupCluster() throws Exception {
    // create and configure cluster
    configureCluster(NUM_SHARDS*NUM_REPLICAS /* nodeCount */)
    .addConfig("conf", configset("cloud-dynamic"))
    .configure();
  }

  @Before
  public void beforeTest() throws Exception {

    // decide collection name ...
    COLLECTION = "collection"+(1+random().nextInt(100)) ;

    // create an empty collection
    CollectionAdminRequest
    .createCollection(COLLECTION, "conf", NUM_SHARDS, NUM_REPLICAS)
    .processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);
    AbstractDistribZkTestBase.waitForRecoveriesToFinish(COLLECTION, cluster.getSolrClient().getZkStateReader(), false, true, DEFAULT_TIMEOUT);
  }

  @After
  public void afterTest() throws Exception {
    CollectionAdminRequest
    .deleteCollection(COLLECTION)
    .process(cluster.getSolrClient());
  }

  @Test
  public void test() throws Exception {

    int specialIdx = 0;

    final List<SolrClient> solrClients = new ArrayList<>();
    for (JettySolrRunner jettySolrRunner : cluster.getJettySolrRunners()) {
      if (!jettySolrRunner.getBaseUrl().toString().equals(
          getCollectionState(COLLECTION).getLeader("shard1").getBaseUrl())) {
        specialIdx = solrClients.size();
      }
      solrClients.add(jettySolrRunner.newClient());
    }

    cluster.getJettySolrRunner(specialIdx).stop();
    AbstractDistribZkTestBase.waitForRecoveriesToFinish(COLLECTION, cluster.getSolrClient().getZkStateReader(), false, true, DEFAULT_TIMEOUT);

    new UpdateRequest()
    .add(sdoc("id", "1", "a_t", "one"))
    .deleteById("2")
    .deleteByQuery("a_t:three")
    .commit(cluster.getSolrClient(), COLLECTION);

    cluster.getJettySolrRunner(specialIdx).start();
    AbstractDistribZkTestBase.waitForRecoveriesToFinish(COLLECTION, cluster.getSolrClient().getZkStateReader(), false, true, DEFAULT_TIMEOUT);

    int idx = 0;
    for (SolrClient solrClient : solrClients) {
      implTest(solrClient, idx==specialIdx ? 0 : 3);
      ++idx;
    }

    for (SolrClient solrClient : solrClients) {
      solrClient.close();
    }

  }

  @SuppressWarnings("unchecked")
  private void implTest(SolrClient solrClient, int numExpected) throws Exception {

    final QueryRequest reqV = new QueryRequest(params("qt","/get", "getVersions","12345"));
    final NamedList<?> rspV = solrClient.request(reqV, COLLECTION);
    final List<Long> versions = (List<Long>)rspV.get("versions");<commit_msg>SOLR-15806: expand UpdateLogCloudTest coverage w.r.t. Ephemeral vs. Standard DirectoryFactory use (#421)<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.handler.component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.cloud.AbstractDistribZkTestBase;
import org.apache.solr.cloud.SolrCloudTestCase;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.MockDirectoryFactory;
import org.apache.solr.core.StandardDirectoryFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class UpdateLogCloudTest extends SolrCloudTestCase {

  private static Boolean expectVersionsAfterRestart = null;
  private static String COLLECTION;
  private static final int NUM_SHARDS = 1;
  private static final int NUM_REPLICAS = 4;

  @BeforeClass
  public static void setupCluster() throws Exception {
    // choose a directory factory
    expectVersionsAfterRestart = random().nextBoolean();
    System.setProperty("solr.directoryFactory",
        (expectVersionsAfterRestart
            ? StandardDirectoryFactory.class.getCanonicalName()
                : MockDirectoryFactory.class.getCanonicalName()));

    // create and configure cluster
    configureCluster(NUM_SHARDS*NUM_REPLICAS /* nodeCount */)
    .addConfig("conf", configset("cloud-dynamic"))
    .configure();
  }

  @Before
  public void beforeTest() throws Exception {

    // decide collection name ...
    COLLECTION = "collection"+(1+random().nextInt(100)) ;

    // create an empty collection
    CollectionAdminRequest
    .createCollection(COLLECTION, "conf", NUM_SHARDS, NUM_REPLICAS)
    .processAndWait(cluster.getSolrClient(), DEFAULT_TIMEOUT);
    AbstractDistribZkTestBase.waitForRecoveriesToFinish(COLLECTION, cluster.getSolrClient().getZkStateReader(), false, true, DEFAULT_TIMEOUT);
  }

  @After
  public void afterTest() throws Exception {
    CollectionAdminRequest
    .deleteCollection(COLLECTION)
    .process(cluster.getSolrClient());
  }

  @Test
  public void test() throws Exception {

    int specialIdx = 0;

    final List<SolrClient> solrClients = new ArrayList<>();
    for (JettySolrRunner jettySolrRunner : cluster.getJettySolrRunners()) {
      if (!jettySolrRunner.getBaseUrl().toString().equals(
          getCollectionState(COLLECTION).getLeader("shard1").getBaseUrl())) {
        specialIdx = solrClients.size();
      }
      solrClients.add(jettySolrRunner.newClient());
    }

    new UpdateRequest()
    .add(sdoc("id", "0", "a_t", "zero"))
    .commit(cluster.getSolrClient(), COLLECTION);

    for (SolrClient solrClient : solrClients) {
      implTest(solrClient, 1);
    }

    cluster.getJettySolrRunner(specialIdx).stop();
    AbstractDistribZkTestBase.waitForRecoveriesToFinish(COLLECTION, cluster.getSolrClient().getZkStateReader(), false, true, DEFAULT_TIMEOUT);

    new UpdateRequest()
    .add(sdoc("id", "1", "a_t", "one"))
    .deleteById("2")
    .deleteByQuery("a_t:three")
    .commit(cluster.getSolrClient(), COLLECTION);

    cluster.getJettySolrRunner(specialIdx).start();
    AbstractDistribZkTestBase.waitForRecoveriesToFinish(COLLECTION, cluster.getSolrClient().getZkStateReader(), false, true, DEFAULT_TIMEOUT);

    int idx = 0;
    for (SolrClient solrClient : solrClients) {
      implTest(solrClient, idx==specialIdx ? (expectVersionsAfterRestart ? 4 : 0) : 4);
      ++idx;
    }

    for (SolrClient solrClient : solrClients) {
      solrClient.close();
    }

  }

  @SuppressWarnings("unchecked")
  private void implTest(SolrClient solrClient, int numExpected) throws Exception {

    final QueryRequest reqV = new QueryRequest(params("qt","/get", "getVersions","12345"));
    final NamedList<?> rspV = solrClient.request(reqV, COLLECTION);
    final List<Long> versions = (List<Long>)rspV.get("versions");
{'fraction_non_alphanumeric': 0.10711561512476844, 'fraction_numerical': 0.0059932439795140026, 'mean_word_length': 4.463095238095238, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2146314', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2714, 'n_words': 658}