Skip to content

Commit

Permalink
Fix #178: Support multiple FastestServerLB instances
Browse files Browse the repository at this point in the history
  • Loading branch information
magro committed Jan 30, 2024
1 parent d020606 commit 7af2dc0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/main/scala/io/ino/solrs/LoadBalancer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import java.util.function.IntUnaryOperator

import io.ino.solrs.LoadBalancer.NoSolrServersAvailableException
import io.ino.solrs.ServerStateChangeObservable.Removed
import io.ino.solrs.ServerStateChangeObservable.StateChange
Expand All @@ -23,8 +22,11 @@ import org.apache.solr.client.solrj.SolrResponse
import org.apache.solr.client.solrj.request.IsUpdateRequest
import org.apache.solr.client.solrj.request.QueryRequest
import org.apache.solr.client.solrj.response.QueryResponse
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.management.InstanceAlreadyExistsException
import javax.management.InstanceNotFoundException
import scala.collection.concurrent.TrieMap
import scala.collection.mutable
import scala.concurrent.duration._
Expand Down Expand Up @@ -184,8 +186,6 @@ class FastestServerLB[F[_]](override val solrServers: SolrServers,

import FastestServerLB._

private val logger = LoggerFactory.getLogger(getClass)

private var client: AsyncSolrClient[F] = _

private val scheduler = Executors.newSingleThreadScheduledExecutor()
Expand Down Expand Up @@ -532,12 +532,24 @@ trait FastestServerLBJmxSupport[F[_]] extends FastestServerLBMBean { self: Faste
import FastestServerLB._
import FastestServerLBJmxSupport._

protected val logger: Logger = LoggerFactory.getLogger(getClass)

private lazy val objectName = newObjectName()

def initJmx(): Unit = {
ManagementFactory.getPlatformMBeanServer.registerMBean(this, ObjName)
try {
ManagementFactory.getPlatformMBeanServer.registerMBean(this, objectName)
} catch {
case e: InstanceAlreadyExistsException => logger.warn("Error while registering MBean", e)
}
}

def shutdownJmx(): Unit = {
ManagementFactory.getPlatformMBeanServer.unregisterMBean(ObjName)
try {
ManagementFactory.getPlatformMBeanServer.unregisterMBean(objectName)
} catch {
case e: InstanceNotFoundException => logger.warn("Error while unregistering MBean", e)
}
}

override def averagesPerSecond(collection: String): TabularData = {
Expand Down Expand Up @@ -615,6 +627,7 @@ trait FastestServerLBJmxSupport[F[_]] extends FastestServerLBMBean { self: Faste

object FastestServerLBJmxSupport {

val ObjName = new ObjectName("io.ino.solrs:type=FastestServerLB")
private val instanceCounter = new AtomicInteger(0)
def newObjectName(): ObjectName = new ObjectName(s"io.ino.solrs:type=FastestServerLB,instance=${instanceCounter.getAndIncrement()}")

}
7 changes: 7 additions & 0 deletions src/test/scala/io/ino/solrs/FastestServerLBSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class FastestServerLBSpec extends StandardFunSpec {

describe("FastestServerLB") {

it("should support multiple instances") {
val servers = IndexedSeq(SolrServer("host1"))
this.cut = newDynamicLB(new StaticSolrServers(servers), q, clock)
val cut2 = newDynamicLB(new StaticSolrServers(servers), q, clock)
cut2.shutdown()
}

it("should return a Failure if no solr server matches") {
val nonMatchingServers = new SolrServers {
override def all: Seq[SolrServer] = Nil
Expand Down

0 comments on commit 7af2dc0

Please sign in to comment.