Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cores and collections management #66

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 142 additions & 17 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
name: Tests
'on': [push]

env:
SOLR_URL: http://localhost:8983/solr/test-core

jobs:
rspec:
rspec_single:
name: Rspec on Ruby ${{ matrix.ruby }} with Solr ${{ matrix.solr }}
runs-on: ubuntu-latest
strategy:
matrix:
ruby: ['2.7', '3.0', '3.2']
solr: ['8.11.2', '9.7.0']
services:
solr:
image: solr:${{ matrix.solr }}
ports:
- 8983:8983
ruby: ['3.2']
solr: ['9.7.0']
env:
SOLR_URL: http://localhost:8983/solr/test-core
steps:
- uses: actions/checkout@v2

Expand All @@ -26,20 +20,151 @@ jobs:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- name: Run docker-compose
uses: hoverkraft-tech/compose-action@v2
env:
SOLR_VERSION: ${{ matrix.solr }}
with:
compose-file: docker-compose.single.yml

- name: Wait for Solr to be ready
timeout-minutes: 2
run: |
echo "Waiting for Solr to be ready..."
while ! curl -s 'http://localhost:8983/solr/admin/info/system' > /dev/null; do
sleep 2
echo "Still waiting for Solr..."
done
echo "Solr is up!"

- name: Prepare default configset
run: |
container_id=$(docker ps -q)
docker exec -u 0 $container_id sh -c "mkdir /var/solr/data/configsets \
&& cp -R /opt/solr/server/solr/configsets/_default /var/solr/data/configsets/ \
&& chown -R solr:solr /var/solr/data/configsets"
container_id=$(docker ps -q --filter name=solrb-solr)
echo "Using container: $container_id"
docker exec -u 0 $container_id sh -c \
"mkdir -p /var/solr/data/configsets/_default && \
cp -R /opt/solr/server/solr/configsets/_default/* /var/solr/data/configsets/_default/ && \
chown -R solr:solr /var/solr/data/configsets && \
ls -la /var/solr/data/configsets/_default/"

- name: Create a test core
run: |
curl 'http://localhost:8983/solr/admin/cores?action=CREATE&name=test-core&configSet=_default'
curl --retry 5 --retry-delay 2 -sS 'http://localhost:8983/solr/admin/cores?action=CREATE&name=test-core&configSet=_default'

- name: Verify core creation
timeout-minutes: 1
run: |
echo "Verifying core creation..."
while ! curl -s 'http://localhost:8983/solr/admin/cores?action=STATUS' | grep -q '"test-core":'; do
sleep 2
echo "Waiting for core to be ready..."
done
echo "Core is ready!"

- name: Disable field type guessing
run: |
curl http://localhost:8983/solr/test-core/config -d '{"set-user-property": {"update.autoCreateFields":"false"}}'

- name: Rspec
run: bundle exec rspec --format progress
run: bundle exec rspec --format progress --tag ~installation:master-replica --tag ~installation:cloud

rspec_master_replica:
name: Rspec Master-Replica on Ruby ${{ matrix.ruby }} with Solr ${{ matrix.solr }}
runs-on: ubuntu-latest
strategy:
matrix:
ruby: ['3.2']
solr: ['9.7.0']
env:
SOLR_MASTER_URL: http://localhost:8983/solr/test-core
SOLR_REPLICA_URL: http://localhost:8984/solr/test-core
steps:
- uses: actions/checkout@v2

- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- name: Run docker-compose
uses: hoverkraft-tech/compose-action@v2
env:
SOLR_VERSION: ${{ matrix.solr }}
with:
compose-file: docker-compose.master-replica.yml

- name: Wait for Solr master to be ready
timeout-minutes: 2
run: |
echo "Waiting for Solr master to be ready..."
while ! curl -s 'http://localhost:8983/solr/admin/info/system' > /dev/null; do
sleep 2
echo "Still waiting for Solr master..."
done
echo "Solr master is up!"

- name: Wait for Solr replica to be ready
timeout-minutes: 2
run: |
echo "Waiting for Solr replica to be ready..."
while ! curl -s 'http://localhost:8984/solr/admin/info/system' > /dev/null; do
sleep 2
echo "Still waiting for Solr replica..."
done
echo "Solr replica is up!"

- name: Prepare master configset
run: |
container_id=$(docker ps -q --filter name=solrb-master-replica-solr-master)
echo "Using master container: $container_id"
docker exec -u 0 $container_id sh -c \
"mkdir -p /var/solr/data/configsets/_default && \
cp -R /opt/solr/server/solr/configsets/_default/* /var/solr/data/configsets/_default/ && \
chown -R solr:solr /var/solr/data/configsets && \
ls -la /var/solr/data/configsets/_default/"

- name: Prepare replica configset
run: |
container_id=$(docker ps -q --filter name=solrb-master-replica-solr-replica)
echo "Using replica container: $container_id"
docker exec -u 0 $container_id sh -c \
"mkdir -p /var/solr/data/configsets/_default && \
cp -R /opt/solr/server/solr/configsets/_default/* /var/solr/data/configsets/_default/ && \
chown -R solr:solr /var/solr/data/configsets && \
ls -la /var/solr/data/configsets/_default/"

- name: Create master core
run: |
curl --retry 5 --retry-delay 2 -sS 'http://localhost:8983/solr/admin/cores?action=CREATE&name=test-core&configSet=_default'

- name: Create replica core
run: |
curl --retry 5 --retry-delay 2 -sS 'http://localhost:8984/solr/admin/cores?action=CREATE&name=test-core&configSet=_default'

- name: Verify cores creation
timeout-minutes: 1
run: |
echo "Verifying master core..."
while ! curl -s 'http://localhost:8983/solr/admin/cores?action=STATUS' | grep -q '"test-core":'; do
sleep 2
echo "Waiting for master core..."
done
echo "Master core is ready!"

echo "Verifying replica core..."
while ! curl -s 'http://localhost:8984/solr/admin/cores?action=STATUS' | grep -q '"test-core":'; do
sleep 2
echo "Waiting for replica core..."
done
echo "Replica core is ready!"

- name: Disable field type guessing on master
run: |
curl http://localhost:8983/solr/test-core/config -d '{"set-user-property": {"update.autoCreateFields":"false"}}'

- name: Disable field type guessing on replica
run: |
curl http://localhost:8984/solr/test-core/config -d '{"set-user-property": {"update.autoCreateFields":"false"}}'

- name: Rspec
run: bundle exec rspec --format progress --tag installation:master_replica --tag ~installation:cloud --tag ~installation:single
101 changes: 99 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Object-Oriented approach to Solr in Ruby.
* [Master-slave](#master-slave)
* [Gray list](#gray-list)
* [Basic Authentication](#basic-authentication)
* [Core Management](#core-management)
* [Creating a Core](#creating-a-core)
* [Managing Cores](#managing-cores)
* [Indexing](#indexing)
* [Querying](#querying)
* [Simple Query](#simple-query)
Expand Down Expand Up @@ -173,8 +176,8 @@ In solr master-slave mode you don't need to provide a solr url (`config.url` or

```ruby
Solr.configure do |config|
config.master_url = 'localhost:8983'
config.slave_url = 'localhost:8984'
config.master_url = 'localhost:8983' # or ENV['SOLR_MASTER_URL']
config.slave_url = 'localhost:8984' # or ENV['SOLR_SLAVE_URL']
# Disable select queries from master:
config.disable_read_from_master = true
# Specify Gray-list service
Expand Down Expand Up @@ -223,6 +226,54 @@ Solr.configure do |config|
end
```

# Core Management

Solrb provides a comprehensive API for managing Solr cores through the `Solr.cores` interface. Here are the available operations:

## Creating a Core

```ruby
# Create a core with default configuration
Solr.cores.create(name: 'my-core')

# Create a core with custom configuration
Solr.cores.create(
name: 'my-core',
config_set: '_default', # Optional, defaults to '_default'
config_dir: 'path/to/config', # Optional
schema_file: 'path/to/schema.xml', # Optional
data_dir: 'path/to/data' # Optional
)
```

## Managing Cores

```ruby
# Check if a core exists
Solr.cores.exists?(name: 'my-core')

# Get status of all cores or a specific core
Solr.cores.status # All cores
Solr.cores.status(name: 'my-core') # Specific core

# Reload a core
Solr.cores.reload(name: 'my-core')

# Rename a core
Solr.cores.rename(name: 'old-name', new_name: 'new-name')

# Unload a core
Solr.cores.unload(name: 'my-core')

# Unload and delete a core completely
Solr.cores.unload(
name: 'my-core',
delete_index: true, # Remove the index
delete_data_dir: true, # Remove the data directory
delete_instance_dir: true # Remove everything related to the core
)
```

# Indexing

```ruby
Expand Down Expand Up @@ -553,6 +604,52 @@ SOLR_URL=http://localhost:8983/solr/test-core rspec
docker-compose -f docker-compose.single.yml down -v
```

### Master-Replica Setup

```sh
# Start Solr master and replica nodes
docker-compose -f docker-compose.master-replica.yml up -d

# Wait for both nodes to be healthy
docker-compose -f docker-compose.master-replica.yml ps

# Setup master node
# First copy the default configset to the correct location on master
docker exec -u 0 solrb-master-replica-solr-master-1 sh -c "mkdir -p /var/solr/data/configsets && cp -R /opt/solr/server/solr/configsets/_default /var/solr/data/configsets/ && chown -R solr:solr /var/solr/data/configsets"

# Create test core on master
curl 'http://localhost:8983/solr/admin/cores?action=CREATE&name=test-core&configSet=_default'

# Setup replica node
# Copy the default configset to the correct location on replica
docker exec -u 0 solrb-master-replica-solr-replica-1 sh -c "mkdir -p /var/solr/data/configsets && cp -R /opt/solr/server/solr/configsets/_default /var/solr/data/configsets/ && chown -R solr:solr /var/solr/data/configsets"

# Create test core on replica
curl 'http://localhost:8984/solr/admin/cores?action=CREATE&name=test-core&configSet=_default'

# Enable replication on master
curl 'http://localhost:8983/solr/test-core/replication?command=enablereplication&master=true'

# Enable replication on replica
curl 'http://localhost:8984/solr/test-core/replication?command=enablereplication&slave=true&masterUrl=http://solr-master:8983/solr/test-core'

# Verify replication status on master
curl 'http://localhost:8983/solr/test-core/replication?command=details'

# Verify replication status on replica
curl 'http://localhost:8984/solr/test-core/replication?command=details'

# Disable field guessing on both nodes
curl http://localhost:8983/solr/test-core/config -d '{"set-user-property": {"update.autoCreateFields":"false"}}'
curl http://localhost:8984/solr/test-core/config -d '{"set-user-property": {"update.autoCreateFields":"false"}}'

# Run specs with master-slave configuration
SOLR_MASTER_URL=http://localhost:8983/solr/test-core SOLR_SLAVE_URL=http://localhost:8984/solr/test-core rspec

# Clean up
docker-compose -f docker-compose.master-replica.yml down -v
```

## Manual Setup with Docker Commands

If you prefer more control or need to debug the setup, you can use the manual Docker commands:
Expand Down
47 changes: 47 additions & 0 deletions docker-compose.master-replica.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: solrb-master-replica

services:
solr-master:
image: solr:${SOLR_VERSION:-9.7.0}-slim
ports:
- "8983:8983"
volumes:
- solr_master_data:/var/solr
environment:
- SOLR_OPTS=-Denable.master=true
networks:
- solr_network
healthcheck:
test: curl --fail http://localhost:8983/solr/admin/info/system
interval: 10s
timeout: 5s
retries: 5
start_period: 20s

solr-replica:
image: solr:${SOLR_VERSION:-9.7.0}-slim
ports:
- "8984:8983"
volumes:
- solr_replica_data:/var/solr
environment:
- SOLR_OPTS=-Denable.replica=true -Dmaster.url=http://solr-master:8983/solr/test-core
networks:
- solr_network
depends_on:
solr-master:
condition: service_healthy
healthcheck:
test: curl --fail http://localhost:8983/solr/admin/info/system
interval: 10s
timeout: 5s
retries: 5
start_period: 20s

networks:
solr_network:
driver: bridge

volumes:
solr_master_data:
solr_replica_data:
10 changes: 5 additions & 5 deletions docker-compose.single.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ version: '3.8'

services:
solr:
image: solr:9.7.0-slim
image: solr:${SOLR_VERSION:-9.7.0}-slim
ports:
- "8983:8983"
volumes:
- solr_single_data:/var/solr
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8983/solr/"]
interval: 5s
test: curl --fail http://localhost:8983/solr/admin/info/system
interval: 10s
timeout: 5s
retries: 3
start_period: 5s
retries: 5
start_period: 20s

volumes:
solr_single_data:
Loading
Loading