Bug 1044294

Summary: 0 byte local cache file in UrlResource
Product: [Retired] JBoss BRMS Platform 6 Reporter: Edson Tirelli <etirelli>
Component: BREAssignee: Edson Tirelli <etirelli>
Status: CLOSED CURRENTRELEASE QA Contact: Marek Winkler <mwinkler>
Severity: high Docs Contact:
Priority: high    
Version: 6.0.0CC: brms-jira, etirelli, mbaluch, mwinkler, rzhang, tkobayas
Target Milestone: ER 7   
Target Release: 6.0.0   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: 1034030 Environment:
Last Closed: 2014-08-06 20:20:10 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Bug Depends On: 1034030    
Bug Blocks:    

Description Edson Tirelli 2013-12-18 03:34:49 UTC
+++ This bug was initially created as a clone of Bug #1034030 +++

Description of problem:

Assuming you configure KnowledgeAgent/ResourceChangeScanner with ChangeSet.xml to connect to Guvnor. In addition, you configure drools.resource.urlcache to enable local file cache.

If the following scenario happens:

1. ResourceChangeScanner scans for Guvnor
2. Calls UrlResource.getInputStream()
2.1 Gets updated lastModified by HTTP HEAD in UrlResource.grabLastMod()
2.2 Calls UrlResource.cacheStream()
2.2.1 Deletes the file cache and open FileOutputStream. (Now the cache file is 0 byte)
2.2.2 *** Guvnor goes down (shutdown/crash/network issue etc.) ***
2.2.3 grabStream() throws an Exception because Guvnor is unreachable
2.2.4 UrlResource.cacheStream() catches the Exception and leaves the 0 byte cache file
2.3 UrlResource.getInputStream() calls UrlResource.grabStream() again and it throws an Exception again
2.4 UrlResource.getInputStream() catches the Exception and reads the 0 byte cache file and returns its InputStream

KnowledgeAgent will keep failing with the 0 byte cache file.

You can reproduce the scenario by setting a breakpoint in UrlResource.cacheStream() before calling grabStream(). Then shutdown Guvnor.

====
    private void cacheStream() {
        try {
            File fi = getCacheFile();
            if (fi.exists()) fi.delete();
            FileOutputStream fout = new FileOutputStream(fi);
  HERE! ==> InputStream in = grabStream();
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int n;
            while (-1 != (n = in.read(buffer))) {
                fout.write(buffer, 0, n);
            }
            fout.flush();
            fout.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
====


Steps to Reproduce:

See above steps.

Actual results:

local cache file becomes 0 byte and KnowledgeAgent will keep failing with the file.

====[2013-11-25 11:49:11,723:exception]
java.lang.RuntimeException: KnowledgeAgent exception while trying to deserialize KnowledgeDefinitionsPackage  
	at org.drools.agent.impl.KnowledgeAgentImpl.createPackageFromResource(KnowledgeAgentImpl.java:774)
	at org.drools.agent.impl.KnowledgeAgentImpl.addResourcesToKnowledgeBase(KnowledgeAgentImpl.java:1069)
	at org.drools.agent.impl.KnowledgeAgentImpl.rebuildResources(KnowledgeAgentImpl.java:822)
	at org.drools.agent.impl.KnowledgeAgentImpl.buildKnowledgeBase(KnowledgeAgentImpl.java:671)
	at org.drools.agent.impl.KnowledgeAgentImpl.applyChangeSet(KnowledgeAgentImpl.java:201)
	at org.drools.agent.impl.KnowledgeAgentImpl$ChangeSetNotificationDetector.run(KnowledgeAgentImpl.java:1268)
	at java.lang.Thread.run(Thread.java:662)
Caused by: java.io.EOFException
	at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
	at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)
	at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)
	at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
	at org.drools.common.DroolsObjectInputStream.<init>(DroolsObjectInputStream.java:68)
	at org.drools.core.util.DroolsStreamUtils.streamIn(DroolsStreamUtils.java:205)
	at org.drools.core.util.DroolsStreamUtils.streamIn(DroolsStreamUtils.java:174)
	at org.drools.agent.impl.KnowledgeAgentImpl.createPackageFromResource(KnowledgeAgentImpl.java:747)
	... 6 more
====

Expected results:

KnowledgeAgent uses old valid file cache until it completely reads updated resources.

Additional info:

--- Additional comment from Toshiya Kobayashi on 2013-11-25 09:16:27 EST ---

Sent a pull request.

https://github.com/droolsjbpm/drools/pull/289

--- Additional comment from Edson Tirelli on 2013-12-17 22:33:54 EST ---

Applied to 5.3.x, 5.6.x and master:

(5.3.x):
http://github.com/droolsjbpm/drools/commit/05196fa8d

(5.6.x):
http://github.com/droolsjbpm/drools/commit/1621e3bb7

(master):
http://github.com/droolsjbpm/drools/commit/d5e335b86

Thank you!

Comment 1 Edson Tirelli 2013-12-18 03:35:58 UTC
Forward porting for from 5.3.x. Waiting for the acks and blocker flags to be set.

Comment 2 Edson Tirelli 2013-12-18 16:32:22 UTC
Cherry-picked into 6.0.x:

https://github.com/droolsjbpm/drools/commit/17efe8b024d1ee811f063aa9d0943e471b6c259b

Comment 3 Marek Winkler 2014-01-14 17:22:38 UTC
Verified on BRMS 6.0.0 ER7.