-
April 3rd, 2008 02:57 AM #1
Synchronized problem on service side.
Hello,
I'm writing a service program that generates some files on the compute host. My program will use these file in 3 steps:
1 OnSessionEnter or OnInvoke generate these files
2 OnInvoke use these files
3 OnSessionLeave or OnServiceDestroy delete these files
And in order to make the service program function right, different SIs on the same compute host must operate these files synchronizely.
I tried to use java synchronized functions to synchronize class around different objects like this:
public class SymService extends ServiceContainer {
private static byte[] lock = new byte[0];
private function1() {
synchronized (lock) {
//.........
}
}
private function2() {
synchronized (lock) {
//.........
}
}
}
But the results confused me that the code could't work properly after several tests. And i also tried synchronized (SymService.class) , it can not function too. The program didn't run synchronizely on the symphony compute host.
So i wonder if the synchronized of class can not work because of the implement of symphony service session. If it can work would you give me some examples about how to synchronized on service side, and if not please explain some details.
Please give me some advice about this problem.
I did these test on SymphonyDE3.1.
I'm looking forward to your reply.
Thank you.
Please give us some help about this, thanks a lot. :-)
Last edited by simonlee; April 3rd, 2008 at 03:00 AM.
-
April 3rd, 2008 02:21 PM #2
Idle resource
Hello Simon,
You cannot synchronize operations on files among multiple SIs on the same compute host let alone different hosts, because SIs do not share state. If a particular SI, say "SI-1", is done with its task, onDestryService() could be called any time when the resource (slot) gets reclaimed with default proportional scheduling policy. Other SIs cannot prevent file deletion from this call in "SI-1", while they still need to use the files.
What you need to do is make sure onDestroyService() or onSessionLeave() calls are invoked only after all SIs used the generated files. Avoid reaching following conditions while SIs are using the files;
If you're deleting files inside onDestroyService() call:- The resource that the task is running on is reclaimed
- The application is configured to proactively release the
resource, by specifying a low-watermark in the
application profile - The application is unregistered or disabled
If you are deleting files inside onSessionLeave() call:- There is no work for the current binding session
- The current binding session is no longer valid
- The service instance is reassigned to a different session
However, even though there is a mechanism to synchronize in the way you desired (using locks), it will be waisting the resource, because one SI will be locking the files and all other SIs will become idle.
Last edited by Young; April 3rd, 2008 at 03:02 PM.
-
April 3rd, 2008 04:34 PM #3
Synchronization
A quick response about synchronization. The Java lock will only synchronize threads within one process. Since each SI runs in a different process, you need to use an inter-process lock to synchronize file access. I know how to do this in C++ but not Java. In C++, you can use flock or semop on Unix and a system mutex on Windows.
Your use case to share files between SI instances isn't very clear to us. Do you want to share data inside a file between SI's from the same session or any session. Symphony doesn't guarantee that all SI's running on the same host will be running tasks in the same session. Also, Symphony will call sessionLeave on an SI as soon as there is no task data available and then re-send sessionEnter when more task data is available. This is the default policy. There is another policy that will leave SI's bound: R_MinimumServices but this policy is not efficient in general.
In a typical case, where task data is not always available, the SI's will be bound and unbound to a session several times. Sharing data in this complicate scenario will be difficult.
Simon, let us know specifically what data you want to share and we may be able to give you a better approach.
- Ajith
-
April 9th, 2008 01:53 PM #4
Thank you for your attentions
Thank you for your attentions, sincerelly.
Here is what i intend to do:
onSessionEnter: init some files
onInvoke: use these files
onSessionLeave or onDestroyService: remove these files
Problems occurs when different SIs using these files on the same host at the very same time.
At first I tried to use java synchronised functions to solve this problem. But as you said the SIs does not share states.
So I try something weird :-).
I use a file to function like a locker like this:
private boolean getLock() {
try {
raf = new RandomAccessFile(new File(this.workDir + "\\locker.lkr"), "rw");
FileChannel fc = raf.getChannel();
fl = fc.tryLock();
if (fl.isValid() == true)
return true;
else return false;
} catch (Exception e) {
return false;
}
}
private void freeLock(String logInfo) {
try {
raf.write(this.constantLogInfo.getBytes());
fl.release();
raf.close();
} catch (Exception e) {
}
}
Fortunately it works. Hope it won't waste too much system resources.
Thank you for your help.
Have a good day.
-
April 9th, 2008 02:23 PM #5
Still some holes in the synchronization?
Glad to hear that it served your purpose. However, in case you have common data, onSessionLeave() can be called before onInvoke() that locks the file. In such case, it is possible that the shared file can be deleted even though there are remaining read/write operations.
-
April 9th, 2008 03:38 PM #6
SI Data Sharing
Hi Simon,
You have to implement some handling for negative cases. There are a lot of multi-core systems in the field and many SI's can execute code in parallel.
1. Two SI's execute OnSessionEnter at exactly the same time. Which one will create the file? You need to implement some locking here to make sure one SI creates the file and the other SI waits.
2. One SI is running OnSessionLeave and another is running OnSessionEnter. Depending on the timing, the files can be deleted and then re-created. Make sure you can handle this case.
3. The SI crashed (or host crash/power out) on OnSessionLeave. How will the files get deleted. Some process should delete the files, but it needs to make sure that the files are not in use.
You need to implement a single-writer and multiple reader lock. No reader can continue if the write-lock is held. If an SI needs to write to the files, then all reading must be stopped. You need to hold the write lock for creation and deletion of the files. There should be a different lock pair for each session.
- Ajith
-
April 10th, 2008 01:18 AM #7
Thank you all for such quick replies.
Your opinion help me a lot. I'm aware of the problems you mentioned.
In case the different SIs from the same service program share these files on one hosts, only one copy of these files are needed.
So a check for files existence would be necessary before generate files. And before deleting any files there will be a check for file using, which is implement by the locker.
In a word, the program ensure that there will be just one creation and deletion function before and after using these files. And different SIs can use these file at the same time. The sequence will be like this:
SI1-------------------------------------SI2---------------------------------------SI3-------------------------------------------SI4
start -------------------------------------start -------------------------------------start-------------------------------------------------
creating files-----------------------block when creating------------------------block when creating-----------------------------------------
using files -------------------------------using files---------------------------------using files----------------------------------------start
skip delete----------------------------deleting files----------------------------------skip delete-------------------------------block when creating
finish--------------------------------------finish---------------------------------------finish--------------------------------------creating files
-----------------------------------------------------------------------------------------------------------------------------------using files
----------------------------------------------------------------------------------------------------------------------------------deleting files
--------------------------------------------------------------------------------------------------------------------------------------finish
Hope that I've made myself clear. And thanks again for your help.
Last edited by simonlee; April 10th, 2008 at 03:14 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules