You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by George News <ge...@gmx.net> on 2016/12/30 10:22:07 UTC

ClosedException on calling Individual.isClass()

Hi all,

I'm facing a really strange problem when concurrently using a model. I have created a REST webservice which is running some tasks on a model sent through a POST.

In this code, I have created an OntModel and add a resource description to it. I loop over all the statements to check and modify some values. For some of that I'm using Individual.isClass() validation.

The OntModel is based on my own spec and I create it like (just a summary):

	private MyOntModelSpec() {
		super(OntModelSpec.OWL_MEM_MICRO_RULE_INF);

		String locatorPath = Thread.currentThread().getContextClassLoader()
		        .getResource("").getPath();

		// Locate the ont-policy.rdf file and the ontologies to be used
		FileManager fm = FileManager.get().clone();
		// TODO: There is a bug to be reported that LocatorFiles are not
		// considered when added
		fm.addLocatorFile(locatorPath);
		OntDocumentManager docMngr =
		        new OntDocumentManager(fm, "etc/my-ont-policy.rdf");

		// Parsing data inside the ont-policy.rdf file
		StreamManager sm = StreamManager.makeDefaultStreamManager();
		sm.addLocator(new LocatorFile(locatorPath));
		StreamManager.setGlobal(sm);

		this.setDocumentManager(docMngr);
	}


	public static OntModelSpec getInstance() {
		if (myOntModelSpec == null) {
			myOntModelSpec = new myOntModelSpec();
		}

		return myOntModelSpec;
	}

Then the OntModel is created as:
	private MyOntModel() {
		ontModelSpec = MyOntModelSpec.getInstance();
		ontModel = ModelFactory.createOntologyModel(ontModelSpec);
		ontModel.read("etc/my.owl", null);
	}

	public static OntModel getInstance() {
		if (myModel == null) {
			myModel = new myOntModel();
		}


		// Automatically load all the ontologies
		// included in the etc/my.owl file
		OntModel m = ModelFactory
		        .createOntologyModel(myModel.getOntModelSpec());
		m.read("etc/my.owl", null);
		return m;
	}

And I use it like:
		OntModel ontModel = MyOntModel.getInstance();
		ontModel.add(inputFromRest);

		// snip
		Individual individual = ontModel.getIndividual("an object URI");
		if (individual.isClass()) {
			// Do whatever
		}


Using it one call to the webservice in a sequential way, there is no problem. However sometime when concurrently calling the webservice I'm getting a "org.apache.jena.shared.ClosedException: already closed" when doing the check/validation over the model.


Caused by: org.apache.jena.shared.ClosedException: already closed
	at org.apache.jena.graph.impl.GraphBase.checkOpen(GraphBase.java:66)
	at org.apache.jena.graph.impl.GraphBase.find(GraphBase.java:240)
	at org.apache.jena.graph.compose.MultiUnion.multiGraphFind(MultiUnion.java:170)
	at org.apache.jena.graph.compose.MultiUnion.graphBaseFind(MultiUnion.java:147)
	at org.apache.jena.graph.impl.GraphBase.find(GraphBase.java:241)
	at org.apache.jena.reasoner.FGraph.findWithContinuation(FGraph.java:69)
	at org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97)
	at org.apache.jena.reasoner.FGraph.findWithContinuation(FGraph.java:69)
	at org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97)
	at org.apache.jena.reasoner.FinderUtil$Cascade.findWithContinuation(FinderUtil.java:114)
	at org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97)
	at org.apache.jena.reasoner.FinderUtil$Cascade.findWithContinuation(FinderUtil.java:114)
	at org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97)
	at org.apache.jena.reasoner.rulesys.FBRuleInfGraph.findDataMatches(FBRuleInfGraph.java:215)
	at org.apache.jena.reasoner.rulesys.impl.TopLevelTripleMatchFrame.<init>(TopLevelTripleMatchFrame.java:45)
	at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.<init>(LPInterpreter.java:140)
	at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.<init>(LPInterpreter.java:100)
	at org.apache.jena.reasoner.rulesys.impl.LPBRuleEngine.generatorFor(LPBRuleEngine.java:278)
	at org.apache.jena.reasoner.rulesys.impl.ConsumerChoicePointFrame.init(ConsumerChoicePointFrame.java:78)
	at org.apache.jena.reasoner.rulesys.impl.ConsumerChoicePointFrame.<init>(ConsumerChoicePointFrame.java:66)
	at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.setupTabledCall(LPInterpreter.java:675)
	at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.run(LPInterpreter.java:568)
	at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.next(LPInterpreter.java:190)
	at org.apache.jena.reasoner.rulesys.impl.Generator.pump(Generator.java:252)
	at org.apache.jena.reasoner.rulesys.impl.Generator.pump(Generator.java:239)
	at org.apache.jena.reasoner.rulesys.impl.LPBRuleEngine.pump(LPBRuleEngine.java:311)
	at org.apache.jena.reasoner.rulesys.impl.LPTopGoalIterator.moveForward(LPTopGoalIterator.java:107)
	at org.apache.jena.reasoner.rulesys.impl.LPTopGoalIterator.hasNext(LPTopGoalIterator.java:223)
	at org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
	at org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
	at org.apache.jena.util.iterator.FilterIterator.hasNext(FilterIterator.java:55)
	at org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
	at org.apache.jena.util.iterator.FilterIterator.hasNext(FilterIterator.java:55)
	at org.apache.jena.graph.impl.GraphBase.containsByFind(GraphBase.java:299)
	at org.apache.jena.graph.impl.GraphBase.graphBaseContains(GraphBase.java:277)
	at org.apache.jena.graph.impl.GraphBase.contains(GraphBase.java:269)
	at org.apache.jena.graph.impl.GraphBase.contains(GraphBase.java:286)
	at org.apache.jena.ontology.impl.OWLProfile$SupportsCheck.hasType(OWLProfile.java:336)
	at org.apache.jena.ontology.impl.OWLProfile$3.doCheck(OWLProfile.java:390)
	at org.apache.jena.ontology.impl.OWLProfile.isSupported(OWLProfile.java:257)
	at org.apache.jena.ontology.impl.OntClassImpl$1.canWrap(OntClassImpl.java:90)
	at org.apache.jena.enhanced.EnhNode.canSupport(EnhNode.java:168)
	at org.apache.jena.enhanced.EnhNode.canAs(EnhNode.java:113)
	at org.apache.jena.ontology.impl.OntResourceImpl.isClass(OntResourceImpl.java:1305)
	at mypackage.validateInput(MyClass.java:55)

I don't know why it is happening. Could you give me some hint?

TA for any help.
Jorge



Re: ClosedException on calling Individual.isClass()

Posted by Andy Seaborne <an...@apache.org>.
 > sometime when concurrently calling the webservice

Having code fragment does not help here - could it be that one request 
is doing

  Individual.isClass()

while another is closing the model?

if so, why is one request closing a shared object? Either keep the 
object open, or make a fresh object per request.

	Andy


On 30/12/16 10:22, George News wrote:
> Hi all,
>
> I'm facing a really strange problem when concurrently using a model. I have created a REST webservice which is running some tasks on a model sent through a POST.
>
> In this code, I have created an OntModel and add a resource description to it. I loop over all the statements to check and modify some values. For some of that I'm using Individual.isClass() validation.
>
> The OntModel is based on my own spec and I create it like (just a summary):
>
> 	private MyOntModelSpec() {
> 		super(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
>
> 		String locatorPath = Thread.currentThread().getContextClassLoader()
> 		        .getResource("").getPath();
>
> 		// Locate the ont-policy.rdf file and the ontologies to be used
> 		FileManager fm = FileManager.get().clone();
> 		// TODO: There is a bug to be reported that LocatorFiles are not
> 		// considered when added
> 		fm.addLocatorFile(locatorPath);
> 		OntDocumentManager docMngr =
> 		        new OntDocumentManager(fm, "etc/my-ont-policy.rdf");
>
> 		// Parsing data inside the ont-policy.rdf file
> 		StreamManager sm = StreamManager.makeDefaultStreamManager();
> 		sm.addLocator(new LocatorFile(locatorPath));
> 		StreamManager.setGlobal(sm);
>
> 		this.setDocumentManager(docMngr);
> 	}
>
>
> 	public static OntModelSpec getInstance() {
> 		if (myOntModelSpec == null) {
> 			myOntModelSpec = new myOntModelSpec();
> 		}
>
> 		return myOntModelSpec;
> 	}
>
> Then the OntModel is created as:
> 	private MyOntModel() {
> 		ontModelSpec = MyOntModelSpec.getInstance();
> 		ontModel = ModelFactory.createOntologyModel(ontModelSpec);
> 		ontModel.read("etc/my.owl", null);
> 	}
>
> 	public static OntModel getInstance() {
> 		if (myModel == null) {
> 			myModel = new myOntModel();
> 		}
>
>
> 		// Automatically load all the ontologies
> 		// included in the etc/my.owl file
> 		OntModel m = ModelFactory
> 		        .createOntologyModel(myModel.getOntModelSpec());
> 		m.read("etc/my.owl", null);
> 		return m;
> 	}
>
> And I use it like:
> 		OntModel ontModel = MyOntModel.getInstance();
> 		ontModel.add(inputFromRest);
>
> 		// snip
> 		Individual individual = ontModel.getIndividual("an object URI");
> 		if (individual.isClass()) {
> 			// Do whatever
> 		}
>
>
> Using it one call to the webservice in a sequential way, there is no problem. However sometime when concurrently calling the webservice I'm getting a "org.apache.jena.shared.ClosedException: already closed" when doing the check/validation over the model.
>
>
> Caused by: org.apache.jena.shared.ClosedException: already closed
> 	at org.apache.jena.graph.impl.GraphBase.checkOpen(GraphBase.java:66)
> 	at org.apache.jena.graph.impl.GraphBase.find(GraphBase.java:240)
> 	at org.apache.jena.graph.compose.MultiUnion.multiGraphFind(MultiUnion.java:170)
> 	at org.apache.jena.graph.compose.MultiUnion.graphBaseFind(MultiUnion.java:147)
> 	at org.apache.jena.graph.impl.GraphBase.find(GraphBase.java:241)
> 	at org.apache.jena.reasoner.FGraph.findWithContinuation(FGraph.java:69)
> 	at org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97)
> 	at org.apache.jena.reasoner.FGraph.findWithContinuation(FGraph.java:69)
> 	at org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97)
> 	at org.apache.jena.reasoner.FinderUtil$Cascade.findWithContinuation(FinderUtil.java:114)
> 	at org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97)
> 	at org.apache.jena.reasoner.FinderUtil$Cascade.findWithContinuation(FinderUtil.java:114)
> 	at org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97)
> 	at org.apache.jena.reasoner.rulesys.FBRuleInfGraph.findDataMatches(FBRuleInfGraph.java:215)
> 	at org.apache.jena.reasoner.rulesys.impl.TopLevelTripleMatchFrame.<init>(TopLevelTripleMatchFrame.java:45)
> 	at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.<init>(LPInterpreter.java:140)
> 	at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.<init>(LPInterpreter.java:100)
> 	at org.apache.jena.reasoner.rulesys.impl.LPBRuleEngine.generatorFor(LPBRuleEngine.java:278)
> 	at org.apache.jena.reasoner.rulesys.impl.ConsumerChoicePointFrame.init(ConsumerChoicePointFrame.java:78)
> 	at org.apache.jena.reasoner.rulesys.impl.ConsumerChoicePointFrame.<init>(ConsumerChoicePointFrame.java:66)
> 	at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.setupTabledCall(LPInterpreter.java:675)
> 	at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.run(LPInterpreter.java:568)
> 	at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.next(LPInterpreter.java:190)
> 	at org.apache.jena.reasoner.rulesys.impl.Generator.pump(Generator.java:252)
> 	at org.apache.jena.reasoner.rulesys.impl.Generator.pump(Generator.java:239)
> 	at org.apache.jena.reasoner.rulesys.impl.LPBRuleEngine.pump(LPBRuleEngine.java:311)
> 	at org.apache.jena.reasoner.rulesys.impl.LPTopGoalIterator.moveForward(LPTopGoalIterator.java:107)
> 	at org.apache.jena.reasoner.rulesys.impl.LPTopGoalIterator.hasNext(LPTopGoalIterator.java:223)
> 	at org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
> 	at org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
> 	at org.apache.jena.util.iterator.FilterIterator.hasNext(FilterIterator.java:55)
> 	at org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
> 	at org.apache.jena.util.iterator.FilterIterator.hasNext(FilterIterator.java:55)
> 	at org.apache.jena.graph.impl.GraphBase.containsByFind(GraphBase.java:299)
> 	at org.apache.jena.graph.impl.GraphBase.graphBaseContains(GraphBase.java:277)
> 	at org.apache.jena.graph.impl.GraphBase.contains(GraphBase.java:269)
> 	at org.apache.jena.graph.impl.GraphBase.contains(GraphBase.java:286)
> 	at org.apache.jena.ontology.impl.OWLProfile$SupportsCheck.hasType(OWLProfile.java:336)
> 	at org.apache.jena.ontology.impl.OWLProfile$3.doCheck(OWLProfile.java:390)
> 	at org.apache.jena.ontology.impl.OWLProfile.isSupported(OWLProfile.java:257)
> 	at org.apache.jena.ontology.impl.OntClassImpl$1.canWrap(OntClassImpl.java:90)
> 	at org.apache.jena.enhanced.EnhNode.canSupport(EnhNode.java:168)
> 	at org.apache.jena.enhanced.EnhNode.canAs(EnhNode.java:113)
> 	at org.apache.jena.ontology.impl.OntResourceImpl.isClass(OntResourceImpl.java:1305)
> 	at mypackage.validateInput(MyClass.java:55)
>
> I don't know why it is happening. Could you give me some hint?
>
> TA for any help.
> Jorge
>
>

Re: ClosedException on calling Individual.isClass()

Posted by George News <ge...@gmx.net>.
On 02/01/2017 13:57, A. Soroka wrote:
>>> Since all your models seem to be in-memory then you could simply drop
>>> the close(), it's not necessary here.
>>
>> Are there any kind of models available in current Jena version? I was storing the Dataset using TDB. Then I call getNamedModel() on the dataset and work with it or just with the in-memory.
>>
>> Is there any way to use another thing different that the file based TDB?
> 
> If you actually need disk persistence for the models in your dataset, TDB is your best choice. If they are in-memory only, try using a TIM (transactional in-memory) dataset:
> 
> https://jena.apache.org/documentation/rdf/datasets.html
> 
> or if they are not part of a dataset, even just an in-memory Model.

Thanks a nice to know ;) I'm still stuck with the CloseException :(

> ---
> A. Soroka
> The University of Virginia Library
> 
>> On Jan 2, 2017, at 7:37 AM, George News <ge...@gmx.net> wrote:
>>
>>
>> On 02/01/2017 13:00, Dave Reynolds wrote:
>>>>
>>>> Yes it does. I want to speedup things, as ontologies are not changing.
>>>>
>>>> <DocumentManagerPolicy>
>>>>    <!-- policy for controlling the document manager's behaviour -->
>>>>    <processImports rdf:datatype="&xsd;boolean">true</processImports>
>>>>    <cacheModels rdf:datatype="&xsd;boolean">true</cacheModels>
>>>> </DocumentManagerPolicy>
>>>>
>>>>> Does the code you haven't shown us close your OntModels somewhere?
>>>>
>>>> I call OntModel close on local models vars. But each time I'm creating a
>>>> new model, so don't understand why it's being closed.
>>>
>>> It sounds like your OntModels include imports. These are implemented as
>>> separate sub-models which are bound into a multi-union. If you close the
>>> OntModel that will (I think) close all the imported models.
>>>
>>> If some of those imported models are cached (which seems to be the case
>>> with your setup) and thus shared with other OntModels then you will be
>>> closing submodels of other concurrent OntModel instances. That would
>>> explain your symptoms.
>>
>> I have removed the cached and now it takes ages to perform stuff. As an
>> example, something that took 20 sec now it takes 170 sec. I cannot go
>> for that solution.
>>
>>>
>>> Since all your models seem to be in-memory then you could simply drop
>>> the close(), it's not necessary here.
>>
>> Are there any kind of models available in current Jena version? I was
>> storing the Dataset using TDB. Then I call getNamedModel() on the
>> dataset and work with it or just with the in-memory.
>>
>> Is there any way to use another thing different that the file based TDB?
>>
>>>>> However, since FileManager caching doesn't look particularly thread-safe
>>>>> to me then unless you are doing your own synchronization around all the
>>>>> operations that touch it that could also cause problems.
>>>>
>>>> Could you further explain that? I don't fully understand. What operation
>>>> are touch what? The local ontology files are there just in case the
>>>> remote ones are not accessible.
>>>
>>> FileManager doesn't say it is thread safe and from a quick look at the
>>> code it doesn't seem to be. So if you have two different threads which
>>> are concurrently importing models via the same FileManager then the
>>> cache could become corrupted.
>>>
>>> Possible ways round that would include:
>>>
>>> - preloading your FileManager cache will all the relevant imported
>>> models so there's no concurrent update to the cache
>>
>> How can I do that? Sorry for the "silly" question. It seems the easiest
>> way to solve the problem.
>>
>> I'm also thinking on FileManager.setModelCaching(false). Would it make
>> any sense?
>>
>>> - build your own thread-safe filemanager (hmm, don't think the design
>>> allows for that)
>>>
>>> - perform all your local OntModel instantiation from within a suitably
>>> protected critical section so that all the import processing is done by
>>> a single thread
>>>
>>> Dave
>>>
>>>
>>>
> 
> 

Re: ClosedException on calling Individual.isClass()

Posted by "A. Soroka" <aj...@virginia.edu>.
>> Since all your models seem to be in-memory then you could simply drop
>> the close(), it's not necessary here.
> 
> Are there any kind of models available in current Jena version? I was storing the Dataset using TDB. Then I call getNamedModel() on the dataset and work with it or just with the in-memory.
> 
> Is there any way to use another thing different that the file based TDB?

If you actually need disk persistence for the models in your dataset, TDB is your best choice. If they are in-memory only, try using a TIM (transactional in-memory) dataset:

https://jena.apache.org/documentation/rdf/datasets.html

or if they are not part of a dataset, even just an in-memory Model.

---
A. Soroka
The University of Virginia Library

> On Jan 2, 2017, at 7:37 AM, George News <ge...@gmx.net> wrote:
> 
> 
> On 02/01/2017 13:00, Dave Reynolds wrote:
>>> 
>>> Yes it does. I want to speedup things, as ontologies are not changing.
>>> 
>>> <DocumentManagerPolicy>
>>>    <!-- policy for controlling the document manager's behaviour -->
>>>    <processImports rdf:datatype="&xsd;boolean">true</processImports>
>>>    <cacheModels rdf:datatype="&xsd;boolean">true</cacheModels>
>>> </DocumentManagerPolicy>
>>> 
>>>> Does the code you haven't shown us close your OntModels somewhere?
>>> 
>>> I call OntModel close on local models vars. But each time I'm creating a
>>> new model, so don't understand why it's being closed.
>> 
>> It sounds like your OntModels include imports. These are implemented as
>> separate sub-models which are bound into a multi-union. If you close the
>> OntModel that will (I think) close all the imported models.
>> 
>> If some of those imported models are cached (which seems to be the case
>> with your setup) and thus shared with other OntModels then you will be
>> closing submodels of other concurrent OntModel instances. That would
>> explain your symptoms.
> 
> I have removed the cached and now it takes ages to perform stuff. As an
> example, something that took 20 sec now it takes 170 sec. I cannot go
> for that solution.
> 
>> 
>> Since all your models seem to be in-memory then you could simply drop
>> the close(), it's not necessary here.
> 
> Are there any kind of models available in current Jena version? I was
> storing the Dataset using TDB. Then I call getNamedModel() on the
> dataset and work with it or just with the in-memory.
> 
> Is there any way to use another thing different that the file based TDB?
> 
>>>> However, since FileManager caching doesn't look particularly thread-safe
>>>> to me then unless you are doing your own synchronization around all the
>>>> operations that touch it that could also cause problems.
>>> 
>>> Could you further explain that? I don't fully understand. What operation
>>> are touch what? The local ontology files are there just in case the
>>> remote ones are not accessible.
>> 
>> FileManager doesn't say it is thread safe and from a quick look at the
>> code it doesn't seem to be. So if you have two different threads which
>> are concurrently importing models via the same FileManager then the
>> cache could become corrupted.
>> 
>> Possible ways round that would include:
>> 
>> - preloading your FileManager cache will all the relevant imported
>> models so there's no concurrent update to the cache
> 
> How can I do that? Sorry for the "silly" question. It seems the easiest
> way to solve the problem.
> 
> I'm also thinking on FileManager.setModelCaching(false). Would it make
> any sense?
> 
>> - build your own thread-safe filemanager (hmm, don't think the design
>> allows for that)
>> 
>> - perform all your local OntModel instantiation from within a suitably
>> protected critical section so that all the import processing is done by
>> a single thread
>> 
>> Dave
>> 
>> 
>> 


Re: ClosedException on calling Individual.isClass()

Posted by Andy Seaborne <an...@apache.org>.

On 02/01/17 13:53, George News wrote:
> On 02/01/2017 14:43, Andy Seaborne wrote:
>>
>>
>> On 02/01/17 12:37, George News wrote:
>>> Are there any kind of models available in current Jena version? I was
>>> storing the Dataset using TDB. Then I call getNamedModel() on the
>>> dataset and work with it or just with the in-memory.
>>
>> These don't need closing either.  A graph is a stateless projection of
>> the dataset.
>>
>>> I call OntModel close on local models vars. But each time I'm creating a
>>> new model, so don't understand why it's being closed.
>>
>> Presumably because things within that OntModel are shared.
>>
>>     Andy
>>
>>
>
> By removing the close() on any Model/OntModel in the code it seems that
> the exception is not risen. Is it ok not to close the instances? If so,
> I will keep it as it is. I'm afraid I will have memory issues in the future.

For in-memory and in-memory intermediate structures, the GC will deal 
with memory.

>
> Jorge
>

Re: ClosedException on calling Individual.isClass()

Posted by George News <ge...@gmx.net>.
On 02/01/2017 14:43, Andy Seaborne wrote:
> 
> 
> On 02/01/17 12:37, George News wrote:
>> Are there any kind of models available in current Jena version? I was
>> storing the Dataset using TDB. Then I call getNamedModel() on the
>> dataset and work with it or just with the in-memory.
> 
> These don't need closing either.  A graph is a stateless projection of
> the dataset.
> 
>> I call OntModel close on local models vars. But each time I'm creating a
>> new model, so don't understand why it's being closed.
> 
> Presumably because things within that OntModel are shared.
> 
>     Andy
> 
> 

By removing the close() on any Model/OntModel in the code it seems that
the exception is not risen. Is it ok not to close the instances? If so,
I will keep it as it is. I'm afraid I will have memory issues in the future.

Jorge


Re: ClosedException on calling Individual.isClass()

Posted by Andy Seaborne <an...@apache.org>.

On 02/01/17 12:37, George News wrote:
> Are there any kind of models available in current Jena version? I was
> storing the Dataset using TDB. Then I call getNamedModel() on the
> dataset and work with it or just with the in-memory.

These don't need closing either.  A graph is a stateless projection of 
the dataset.

> I call OntModel close on local models vars. But each time I'm creating a
> new model, so don't understand why it's being closed.

Presumably because things within that OntModel are shared.

     Andy


Re: ClosedException on calling Individual.isClass()

Posted by George News <ge...@gmx.net>.
On 02/01/2017 13:00, Dave Reynolds wrote:
>>
>> Yes it does. I want to speedup things, as ontologies are not changing.
>>
>> <DocumentManagerPolicy>
>>     <!-- policy for controlling the document manager's behaviour -->
>>     <processImports rdf:datatype="&xsd;boolean">true</processImports>
>>     <cacheModels rdf:datatype="&xsd;boolean">true</cacheModels>
>> </DocumentManagerPolicy>
>>
>>> Does the code you haven't shown us close your OntModels somewhere?
>>
>> I call OntModel close on local models vars. But each time I'm creating a
>> new model, so don't understand why it's being closed.
> 
> It sounds like your OntModels include imports. These are implemented as
> separate sub-models which are bound into a multi-union. If you close the
> OntModel that will (I think) close all the imported models.
> 
> If some of those imported models are cached (which seems to be the case
> with your setup) and thus shared with other OntModels then you will be
> closing submodels of other concurrent OntModel instances. That would
> explain your symptoms.

I have removed the cached and now it takes ages to perform stuff. As an
example, something that took 20 sec now it takes 170 sec. I cannot go
for that solution.

> 
> Since all your models seem to be in-memory then you could simply drop
> the close(), it's not necessary here.

Are there any kind of models available in current Jena version? I was
storing the Dataset using TDB. Then I call getNamedModel() on the
dataset and work with it or just with the in-memory.

Is there any way to use another thing different that the file based TDB?

>>> However, since FileManager caching doesn't look particularly thread-safe
>>> to me then unless you are doing your own synchronization around all the
>>> operations that touch it that could also cause problems.
>>
>> Could you further explain that? I don't fully understand. What operation
>> are touch what? The local ontology files are there just in case the
>> remote ones are not accessible.
> 
> FileManager doesn't say it is thread safe and from a quick look at the
> code it doesn't seem to be. So if you have two different threads which
> are concurrently importing models via the same FileManager then the
> cache could become corrupted.
> 
> Possible ways round that would include:
> 
>  - preloading your FileManager cache will all the relevant imported
> models so there's no concurrent update to the cache

How can I do that? Sorry for the "silly" question. It seems the easiest
way to solve the problem.

I'm also thinking on FileManager.setModelCaching(false). Would it make
any sense?

>  - build your own thread-safe filemanager (hmm, don't think the design
> allows for that)
> 
>  - perform all your local OntModel instantiation from within a suitably
> protected critical section so that all the import processing is done by
> a single thread
> 
> Dave
> 
> 
> 

Re: ClosedException on calling Individual.isClass()

Posted by Dave Reynolds <da...@gmail.com>.
On 02/01/17 11:22, George News wrote:
> On 02/01/2017 11:03, Dave Reynolds wrote:
>> On 02/01/17 09:23, George News wrote:
>>>
>>> On 31/12/2016 16:42, Andy Seaborne wrote:
>>>>
>>>>> sometime when concurrently calling the webservice
>>>>
>>>> Having code fragment does not help here - could it be that one
>>>> request is doing
>>>>
>>>> Individual.isClass()
>>>>
>>>> while another is closing the model?
>>>>
>>>> if so, why is one request closing a shared object? Either keep the
>>>> object open, or make a fresh object per request.
>>>>
>>>> Andy
>>>>
>>>
>>> I know it's difficult.
>>
>> Hmmm.
>>
>>> But could you please have a look at the way I'm
>>> creating the MyOntModelSpec. Is it correct? If yes, then is the model
>>> correctly created that spec?
>>
>> Insufficient information to tell.
>
> Sorry for that, but it's difficult to send all as I cannot disclose the
> full code.
>>
>> Does your "etc/my.owl" switch on model caching?
>
> Yes it does. I want to speedup things, as ontologies are not changing.
>
> <DocumentManagerPolicy>
>     <!-- policy for controlling the document manager's behaviour -->
>     <processImports rdf:datatype="&xsd;boolean">true</processImports>
>     <cacheModels rdf:datatype="&xsd;boolean">true</cacheModels>
> </DocumentManagerPolicy>
>
>> Does the code you haven't shown us close your OntModels somewhere?
>
> I call OntModel close on local models vars. But each time I'm creating a
> new model, so don't understand why it's being closed.

It sounds like your OntModels include imports. These are implemented as 
separate sub-models which are bound into a multi-union. If you close the 
OntModel that will (I think) close all the imported models.

If some of those imported models are cached (which seems to be the case 
with your setup) and thus shared with other OntModels then you will be 
closing submodels of other concurrent OntModel instances. That would 
explain your symptoms.

Since all your models seem to be in-memory then you could simply drop 
the close(), it's not necessary here.

>> However, since FileManager caching doesn't look particularly thread-safe
>> to me then unless you are doing your own synchronization around all the
>> operations that touch it that could also cause problems.
>
> Could you further explain that? I don't fully understand. What operation
> are touch what? The local ontology files are there just in case the
> remote ones are not accessible.

FileManager doesn't say it is thread safe and from a quick look at the 
code it doesn't seem to be. So if you have two different threads which 
are concurrently importing models via the same FileManager then the 
cache could become corrupted.

Possible ways round that would include:

  - preloading your FileManager cache will all the relevant imported 
models so there's no concurrent update to the cache

  - build your own thread-safe filemanager (hmm, don't think the design 
allows for that)

  - perform all your local OntModel instantiation from within a suitably 
protected critical section so that all the import processing is done by 
a single thread

Dave



Re: ClosedException on calling Individual.isClass()

Posted by George News <ge...@gmx.net>.
On 02/01/2017 11:03, Dave Reynolds wrote:
> On 02/01/17 09:23, George News wrote:
>>
>> On 31/12/2016 16:42, Andy Seaborne wrote:
>>>
>>>> sometime when concurrently calling the webservice
>>>
>>> Having code fragment does not help here - could it be that one
>>> request is doing
>>>
>>> Individual.isClass()
>>>
>>> while another is closing the model?
>>>
>>> if so, why is one request closing a shared object? Either keep the
>>> object open, or make a fresh object per request.
>>>
>>> Andy
>>>
>>
>> I know it's difficult.
> 
> Hmmm.
> 
>> But could you please have a look at the way I'm
>> creating the MyOntModelSpec. Is it correct? If yes, then is the model
>> correctly created that spec?
> 
> Insufficient information to tell.

Sorry for that, but it's difficult to send all as I cannot disclose the
full code.
> 
> Does your "etc/my.owl" switch on model caching?

Yes it does. I want to speedup things, as ontologies are not changing.

<DocumentManagerPolicy>
    <!-- policy for controlling the document manager's behaviour -->
    <processImports rdf:datatype="&xsd;boolean">true</processImports>
    <cacheModels rdf:datatype="&xsd;boolean">true</cacheModels>
</DocumentManagerPolicy>

> Does the code you haven't shown us close your OntModels somewhere?

I call OntModel close on local models vars. But each time I'm creating a
new model, so don't understand why it's being closed.

> There's no explicit close in the fragments you've posted but the
> exception suggests you are doing close somewhere - directly or indirectly.
> 
> Your MyOntModelSpec.getInstance is sharing a global singleton instance
> which in turn is sharing the FileManager clone that you set up. If you
> have caching switched on in your config then the cached imports will be
> shared across your models. If you close() one of those OntModels I think
> that will close all the imported submodels including those in the shared
> FileManager cache which could lead to the problem you see.

Umm... maybe that's the issue. I was using caching as otherwise it's
taking longer to create the model, and I want to speed up things.

But, if I run the same webservice and things are done sequentially there
is no problem. The cache is created at the very beginning and if close I
might be getting the same problem.

> None of your code fragments have any sign of locking or synchronization
> so there is at least a race condition in MyOntModelSpec.getInstance
> which might result in your having multiple MyOntModelSpec instances but
> that seems unlikely to be the problem.

I initialize this at the very beginning so don't think is the problem,
as after initialization every Spec should be the same.

> 
> However, since FileManager caching doesn't look particularly thread-safe
> to me then unless you are doing your own synchronization around all the
> operations that touch it that could also cause problems.

Could you further explain that? I don't fully understand. What operation
are touch what? The local ontology files are there just in case the
remote ones are not accessible.



> Dave
> 
> 
> 

Re: ClosedException on calling Individual.isClass()

Posted by Dave Reynolds <da...@gmail.com>.
On 02/01/17 09:23, George News wrote:
>
> On 31/12/2016 16:42, Andy Seaborne wrote:
>>
>>> sometime when concurrently calling the webservice
>>
>> Having code fragment does not help here - could it be that one
>> request is doing
>>
>> Individual.isClass()
>>
>> while another is closing the model?
>>
>> if so, why is one request closing a shared object? Either keep the
>> object open, or make a fresh object per request.
>>
>> Andy
>>
>
> I know it's difficult.

Hmmm.

> But could you please have a look at the way I'm
> creating the MyOntModelSpec. Is it correct? If yes, then is the model
> correctly created that spec?

Insufficient information to tell.

Does your "etc/my.owl" switch on model caching?

Does the code you haven't shown us close your OntModels somewhere? 
There's no explicit close in the fragments you've posted but the 
exception suggests you are doing close somewhere - directly or indirectly.

Your MyOntModelSpec.getInstance is sharing a global singleton instance 
which in turn is sharing the FileManager clone that you set up. If you 
have caching switched on in your config then the cached imports will be 
shared across your models. If you close() one of those OntModels I think 
that will close all the imported submodels including those in the shared 
FileManager cache which could lead to the problem you see.

None of your code fragments have any sign of locking or synchronization 
so there is at least a race condition in MyOntModelSpec.getInstance 
which might result in your having multiple MyOntModelSpec instances but 
that seems unlikely to be the problem.

However, since FileManager caching doesn't look particularly thread-safe 
to me then unless you are doing your own synchronization around all the 
operations that touch it that could also cause problems.

Dave



Re: ClosedException on calling Individual.isClass()

Posted by George News <ge...@gmx.net>.
On 31/12/2016 16:42, Andy Seaborne wrote:
> 
>> sometime when concurrently calling the webservice
> 
> Having code fragment does not help here - could it be that one
> request is doing
> 
> Individual.isClass()
> 
> while another is closing the model?
> 
> if so, why is one request closing a shared object? Either keep the 
> object open, or make a fresh object per request.
> 
> Andy
> 

I know it's difficult. But could you please have a look at the way I'm
creating the MyOntModelSpec. Is it correct? If yes, then is the model
correctly created that spec?

Thanks and happy New Year to you all.
Jorge


> 
> On 30/12/2016 11:22, George News wrote:
>> Hi all,
>> 
>> I'm facing a really strange problem when concurrently using a model.
>> I have created a REST webservice which is running some tasks on a
>> model sent through a POST.
>> 
>> In this code, I have created an OntModel and add a resource
>> description to it. I loop over all the statements to check and modify
>> some values. For some of that I'm using Individual.isClass()
>> validation.
>> 
>> The OntModel is based on my own spec and I create it like (just a
>> summary):
>> 
>> private MyOntModelSpec() { 
>> super(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
>> 
>> String locatorPath = Thread.currentThread().getContextClassLoader() 
>> .getResource("").getPath();
>> 
>> // Locate the ont-policy.rdf file and the ontologies to be used 
>> FileManager fm = FileManager.get().clone(); // TODO: There is a bug
>> to be reported that LocatorFiles are not // considered when added 
>> fm.addLocatorFile(locatorPath); OntDocumentManager docMngr = new
>> OntDocumentManager(fm, "etc/my-ont-policy.rdf");
>> 
>> // Parsing data inside the ont-policy.rdf file StreamManager sm =
>> StreamManager.makeDefaultStreamManager(); sm.addLocator(new
>> LocatorFile(locatorPath)); StreamManager.setGlobal(sm);
>> 
>> this.setDocumentManager(docMngr); }
>> 
>> 
>> public static OntModelSpec getInstance() { if (myOntModelSpec ==
>> null) { myOntModelSpec = new myOntModelSpec(); }
>> 
>> return myOntModelSpec; }
>> 
>> Then the OntModel is created as: private MyOntModel() { ontModelSpec
>> = MyOntModelSpec.getInstance(); ontModel =
>> ModelFactory.createOntologyModel(ontModelSpec); 
>> ontModel.read("etc/my.owl", null); }
>> 
>> public static OntModel getInstance() { if (myModel == null) { myModel
>> = new myOntModel(); }
>> 
>> 
>> // Automatically load all the ontologies // included in the
>> etc/my.owl file OntModel m = ModelFactory 
>> .createOntologyModel(myModel.getOntModelSpec()); m.read("etc/my.owl",
>> null); return m; }
>> 
>> And I use it like: OntModel ontModel = MyOntModel.getInstance(); 
>> ontModel.add(inputFromRest);
>> 
>> // snip Individual individual = ontModel.getIndividual("an object
>> URI"); if (individual.isClass()) { // Do whatever }
>> 
>> 
>> Using it one call to the webservice in a sequential way, there is no
>> problem. However sometime when concurrently calling the webservice
>> I'm getting a "org.apache.jena.shared.ClosedException: already
>> closed" when doing the check/validation over the model.
>> 
>> 
>> Caused by: org.apache.jena.shared.ClosedException: already closed at
>> org.apache.jena.graph.impl.GraphBase.checkOpen(GraphBase.java:66) at
>> org.apache.jena.graph.impl.GraphBase.find(GraphBase.java:240) at
>> org.apache.jena.graph.compose.MultiUnion.multiGraphFind(MultiUnion.java:170)
>>
>> 
> at org.apache.jena.graph.compose.MultiUnion.graphBaseFind(MultiUnion.java:147)
>> at org.apache.jena.graph.impl.GraphBase.find(GraphBase.java:241) at
>> org.apache.jena.reasoner.FGraph.findWithContinuation(FGraph.java:69) 
>> at
>> org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97) 
>> at
>> org.apache.jena.reasoner.FGraph.findWithContinuation(FGraph.java:69) 
>> at
>> org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97) 
>> at
>> org.apache.jena.reasoner.FinderUtil$Cascade.findWithContinuation(FinderUtil.java:114)
>>
>> 
> at org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97)
>> at
>> org.apache.jena.reasoner.FinderUtil$Cascade.findWithContinuation(FinderUtil.java:114)
>>
>> 
> at org.apache.jena.reasoner.FinderUtil$Cascade.find(FinderUtil.java:97)
>> at
>> org.apache.jena.reasoner.rulesys.FBRuleInfGraph.findDataMatches(FBRuleInfGraph.java:215)
>>
>> 
> at org.apache.jena.reasoner.rulesys.impl.TopLevelTripleMatchFrame.<init>(TopLevelTripleMatchFrame.java:45)
>> at
>> org.apache.jena.reasoner.rulesys.impl.LPInterpreter.<init>(LPInterpreter.java:140)
>>
>> 
> at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.<init>(LPInterpreter.java:100)
>> at
>> org.apache.jena.reasoner.rulesys.impl.LPBRuleEngine.generatorFor(LPBRuleEngine.java:278)
>>
>> 
> at org.apache.jena.reasoner.rulesys.impl.ConsumerChoicePointFrame.init(ConsumerChoicePointFrame.java:78)
>> at
>> org.apache.jena.reasoner.rulesys.impl.ConsumerChoicePointFrame.<init>(ConsumerChoicePointFrame.java:66)
>>
>> 
> at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.setupTabledCall(LPInterpreter.java:675)
>> at
>> org.apache.jena.reasoner.rulesys.impl.LPInterpreter.run(LPInterpreter.java:568)
>>
>> 
> at org.apache.jena.reasoner.rulesys.impl.LPInterpreter.next(LPInterpreter.java:190)
>> at
>> org.apache.jena.reasoner.rulesys.impl.Generator.pump(Generator.java:252)
>>
>> 
> at org.apache.jena.reasoner.rulesys.impl.Generator.pump(Generator.java:239)
>> at
>> org.apache.jena.reasoner.rulesys.impl.LPBRuleEngine.pump(LPBRuleEngine.java:311)
>>
>> 
> at org.apache.jena.reasoner.rulesys.impl.LPTopGoalIterator.moveForward(LPTopGoalIterator.java:107)
>> at
>> org.apache.jena.reasoner.rulesys.impl.LPTopGoalIterator.hasNext(LPTopGoalIterator.java:223)
>>
>> 
> at org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
>> at
>> org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
>>
>> 
> at org.apache.jena.util.iterator.FilterIterator.hasNext(FilterIterator.java:55)
>> at
>> org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
>>
>> 
> at org.apache.jena.util.iterator.FilterIterator.hasNext(FilterIterator.java:55)
>> at
>> org.apache.jena.graph.impl.GraphBase.containsByFind(GraphBase.java:299)
>>
>> 
> at org.apache.jena.graph.impl.GraphBase.graphBaseContains(GraphBase.java:277)
>> at org.apache.jena.graph.impl.GraphBase.contains(GraphBase.java:269) 
>> at org.apache.jena.graph.impl.GraphBase.contains(GraphBase.java:286) 
>> at
>> org.apache.jena.ontology.impl.OWLProfile$SupportsCheck.hasType(OWLProfile.java:336)
>>
>> 
> at org.apache.jena.ontology.impl.OWLProfile$3.doCheck(OWLProfile.java:390)
>> at
>> org.apache.jena.ontology.impl.OWLProfile.isSupported(OWLProfile.java:257)
>>
>> 
> at org.apache.jena.ontology.impl.OntClassImpl$1.canWrap(OntClassImpl.java:90)
>> at org.apache.jena.enhanced.EnhNode.canSupport(EnhNode.java:168) at
>> org.apache.jena.enhanced.EnhNode.canAs(EnhNode.java:113) at
>> org.apache.jena.ontology.impl.OntResourceImpl.isClass(OntResourceImpl.java:1305)
>>
>> 
> at mypackage.validateInput(MyClass.java:55)
>> 
>> I don't know why it is happening. Could you give me some hint?
>> 
>> TA for any help. Jorge
>> 
>> 
>>