# Customer scorer thread safety

**URL:** <https://discuss.elastic.co/t/customer-scorer-thread-safety/8375>\
**Category:** Elasticsearch\
**Created:** [July 10, 2012, 10:40am UTC](https://discuss.elastic.co/t/customer-scorer-thread-safety/8375 "2012-07-10T10:40:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aidan\_Skinner](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Aidan\_Skinner](https://discuss.elastic.co/u/Aidan_Skinner)\
**Post date:** [July 10, 2012, 10:40am UTC](https://discuss.elastic.co/t/customer-scorer-thread-safety/8375/1 "2012-07-10T10:40:21Z")

</div>

Hi,

I'm implementing a custom scorer plugin which involves calling out to a  
separate process (in this case R) so needs to manage a TCP socket and I was  
wondering if I'd understood the lifecycle and contract right  
for AbstractSearchScript (well, AbstractFloatSearchScript), AIUI:

There's 1 instance per shard (and the live for the life time of the  
shard?), created from the factory

and

runAsFoo needs to be threadsafe but not fully reentrant

Is that right? I'm not entirely sure I followed the logic up to  
shardOperation correctly...

Thanks,

- Aidan

---

<div class="post-metadata">

**Author:** ![Aidan\_Skinner](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Aidan\_Skinner](https://discuss.elastic.co/u/Aidan_Skinner)\
**Post date:** [July 10, 2012, 10:41am UTC](https://discuss.elastic.co/t/customer-scorer-thread-safety/8375/2 "2012-07-10T10:41:17Z")

</div>

(Urgh, sorry, subject should be custom scorer, I've clearly not had enough  
coffee)

- Aidan

---

<div class="post-metadata">

**Author:** ![Colin\_Dellow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colin_dellow/32/2818_2.png) [@Colin\_Dellow](https://discuss.elastic.co/u/Colin_Dellow)\
**Post date:** [July 10, 2012, 3:02pm UTC](https://discuss.elastic.co/t/customer-scorer-thread-safety/8375/3 "2012-07-10T15:02:49Z")

</div>

On Tuesday, 10 July 2012 06:40:21 UTC-4, Aidan Skinner wrote:

> Hi,
> 
> I'm implementing a custom scorer plugin which involves calling out to a  
> separate process (in this case R) so needs to manage a TCP socket and I was  
> wondering if I'd understood the lifecycle and contract right  
> for AbstractSearchScript (well, AbstractFloatSearchScript), AIUI:
> 
> There's 1 instance per shard (and the live for the life time of the  
> shard?), created from the factory

I haven't looked at the ES code, but my experience is 1 instance of the  
factory per shard, with lifetime equal to the shard's lifetime. And then  
one instance of the script per query per shard, with lifetime equal to the  
lifetime of the query.

---

<div class="post-metadata">

**Author:** ![Aidan\_Skinner](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Aidan\_Skinner](https://discuss.elastic.co/u/Aidan_Skinner)\
**Post date:** [July 10, 2012, 9:15pm UTC](https://discuss.elastic.co/t/customer-scorer-thread-safety/8375/4 "2012-07-10T21:15:48Z")

</div>

On Tuesday, July 10, 2012 4:02:49 PM UTC+1, Colin Dellow wrote:

> On Tuesday, 10 July 2012 06:40:21 UTC-4, Aidan Skinner wrote:
> 
> > Hi,
> > 
> > I'm implementing a custom scorer plugin which involves calling out to a  
> > separate process (in this case R) so needs to manage a TCP socket and I was  
> > wondering if I'd understood the lifecycle and contract right  
> > for AbstractSearchScript (well, AbstractFloatSearchScript), AIUI:
> > 
> > There's 1 instance per shard (and the live for the life time of the  
> > shard?), created from the factory
> 
> I haven't looked at the ES code, but my experience is 1 instance of the  
> factory per shard, with lifetime equal to the shard's lifetime. And then  
> one instance of the script per query per shard, with lifetime equal to the  
> lifetime of the query.

That doesn't quite fit with what I've observed with the debugger and  
logging but I'm prepared to believe I'm doing it wrong 🙂

Any idea about threadsafety of runAs?

- Aidan

---

<div class="post-metadata">

**Author:** ![Aidan\_Skinner](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Aidan\_Skinner](https://discuss.elastic.co/u/Aidan_Skinner)\
**Post date:** [July 10, 2012, 10:29pm UTC](https://discuss.elastic.co/t/customer-scorer-thread-safety/8375/5 "2012-07-10T22:29:39Z")

</div>

On Tuesday, July 10, 2012 10:15:48 PM UTC+1, Aidan Skinner wrote:

> On Tuesday, July 10, 2012 4:02:49 PM UTC+1, Colin Dellow wrote:
> 
> > On Tuesday, 10 July 2012 06:40:21 UTC-4, Aidan Skinner wrote:
> > 
> > > Hi,
> > > 
> > > I'm implementing a custom scorer plugin which involves calling out to a  
> > > separate process (in this case R) so needs to manage a TCP socket and I was  
> > > wondering if I'd understood the lifecycle and contract right  
> > > for AbstractSearchScript (well, AbstractFloatSearchScript), AIUI:
> > > 
> > > There's 1 instance per shard (and the live for the life time of the  
> > > shard?), created from the factory
> > 
> > I haven't looked at the ES code, but my experience is 1 instance of the  
> > factory per shard, with lifetime equal to the shard's lifetime. And then  
> > one instance of the script per query per shard, with lifetime equal to the  
> > lifetime of the query.
> 
> That doesn't quite fit with what I've observed with the debugger and  
> logging but I'm prepared to believe I'm doing it wrong 🙂
> 
> Any idea about threadsafety of runAs?

I hassled kimchy on irc, turns out runAs doesn't need to be threadsafe and  
there's one instance of the search script per shard per query.

- Aidan (answering his own mail like a... not a boss... something else)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:20am UTC](https://discuss.elastic.co/t/customer-scorer-thread-safety/8375/6 "2017-07-06T03:20:39Z")

</div>

Closed on July 6, 2017, 3:20am UTC


