Page 1 of 1

Room File Persistence is not working for 2 zone extensions

Posted: 09 Jun 2021, 07:42
by shahidbits2012
We are trying to use persist the Room data via Room Persistence API (FILE) using an [article][http://docs2x.smartfoxserver.com/AdvancedTopics/room-persistence-api].

It works fine when we have one Zone with ZoneExtension=MyZoneExtension. But, if we add another zone which also has the same zone extension i.e. MyZoneExtension, we get the following error while initialising the second zone -

com.smartfoxserver.v2.persistence.room.FileRoomStorage.getFileList(FileRoomStorage.java:306) com.smartfoxserver.v2.persistence.room.FileRoomStorage.loadAllRooms(FileRoomStorage.java:121) com.smartfoxserver.v2.persistence.room.FileRoomStorage.loadAllRooms(FileRoomStorage.java:111) com.example.MyExtension.initializeRooms(MyExtension.java:132)
com.example.MyExtension.init(MyExtension.java:114) com.smartfoxserver.v2.entities.managers.SFSExtensionManager.createExtension(SFSExtensionManager.java:303) com.smartfoxserver.v2.entities.managers.SFSZoneManager.createZone(SFSZoneManager.java:428) com.smartfoxserver.v2.entities.managers.SFSZoneManager.initializeZones(SFSZoneManager.java:239) com.smartfoxserver.v2.SmartFoxServer.start(SmartFoxServer.java:292) com.smartfoxserver.v2.Main.main(Main.java:13)

Can we please get help in resolving this issue?

Thanks!

Re: Room File Persistence is not working for 2 zone extensions

Posted: 09 Jun 2021, 10:04
by Lapo
Hi,
can you specify which version of SFS2X you're running?

Also the stack trace is incomplete as there's no error message. Can you please re-post the whole stack trace?

Thanks

Re: Room File Persistence is not working for 2 zone extensions

Posted: 09 Jun 2021, 11:52
by shahidbits2012
server version: 2.17.0
remote administration: v3.2.6

It is an NPE thrown at below location -

Class: com.smartfoxserver.v2.persistence.room.FileRoomStorage
Method: getFileList(String groupId)

...
File[] var9 = allFiles; // allFiles is null
int var8 = allFiles.length; // Null Pointer Exception
...

Hope this helps!

Re: Room File Persistence is not working for 2 zone extensions

Posted: 10 Jun 2021, 06:45
by Lapo
Thanks for the details. We'll test this use case and let you know asap.

Stay tuned.

Re: Room File Persistence is not working for 2 zone extensions

Posted: 11 Jun 2021, 07:09
by Lapo
We tested the scenario by instantiating two file-based room storage classes in two different Zones but we didn't encounter any issues.

I'd like to see the init() method in your FitplusExtension to better understand what you're doing

Thanks

Re: Room File Persistence is not working for 2 zone extensions

Posted: 11 Jun 2021, 14:12
by shahidbits2012

Code: Select all

/// init()
    @Override
    public void init() {
        ...
        initializeRooms();
    }

    /// method to initialize rooms, if present
    private void initializeRooms() {

        //Initialize Persistence API
        FileRoomStorageConfig cfg = new FileRoomStorageConfig();
        getParentZone().initRoomPersistence(RoomStorageMode.FILE_STORAGE, cfg);

        // Load all previously stored Rooms
        try {
            List<CreateRoomSettings> allRooms = getParentZone().getRoomPersistenceApi().loadAllRooms();

            // Recreate all Rooms
            for (CreateRoomSettings settings : allRooms) {
                getApi().createRoom(getParentZone(), settings, null, false, null, false, false);
            }
        } catch (SFSStorageException storageErr) {
            trace("Error loading rooms: " + storageErr);
        } catch (SFSCreateRoomException creationErr) {
            trace("Error creating room: " + creationErr);
        }

        Thread thread = new ShutdownHookThread();
        Runtime.getRuntime().addShutdownHook(thread);
    }

    /// Thread to save rooms during extension shutdown
    class ShutdownHookThread extends Thread {

        public void run() {

            try {
                  getParentZone().getRoomPersistenceApi().saveAllRooms();
                  trace("Save game rooms success!");
            } catch (SFSStorageException e) {
                  trace("Error saving rooms: " + e.getMessage());
            }
        }
    }

Re: Room File Persistence is not working for 2 zone extensions

Posted: 11 Jun 2021, 23:19
by shahidbits2012
Also, can you try testing with just single class and use the same extension class as the zone extension for both the Zones in admin panel.

Re: Room File Persistence is not working for 2 zone extensions

Posted: 12 Jun 2021, 08:02
by Lapo
Hi,
we have done a few more tests and found an issue that only manifests the first time you run this code. From the second time it doesn't happen anymore.

The reason for this is: the first time you initialize the Room Storage for a specific Zone a new folder must be created under data/roomData.
Here we found a bug that doesn't check for the existence of this folder and causes the exception you have reported. However the folder is created a bit later and the next time you restart the exception the error will not appear.

This problem happens regardless of the number of Zones that use the Room Storage and in fact it manifests also when using it with a single Zone.
We have already fixed the problem in our codebase, but we'll probably publish the update a bit later when we have more fixes and updates to release. The workaround for this problem is to simply let it fail the first time and it won't appear from the next restart.

If you want we can also send a custom update for this issue if it is particularly critical for you. In this case please contact us via the support@... email box with a reference to this post.

Hope it helps