Dynamic World/Channel deployment + Channel scheduler update
Added "8-slot SETUP expand" item on the CashShop. Solved a concurrency issue on the fameGainByQuest method. Refactored many resource freeing modules throughout the source code. Implemented dynamic deployment of worlds and channels on the server system. Only creation of channels and worlds are available on this feature. Added a dedicated worker for schedules requested on EventManager. Fixed a potential cause for deadlocks on the channel schedulers' system. Refactored many schedules used by the EventManager and Channel, futher improving overall scheduling performance on the server.
This commit is contained in:
@@ -34,7 +34,6 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Iterator;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
|
||||
@@ -93,8 +92,8 @@ public class EventInstanceManager {
|
||||
private final ReadLock rL = lock.readLock();
|
||||
private final WriteLock wL = lock.writeLock();
|
||||
|
||||
private final Lock pL = new MonitoredReentrantLock(MonitoredLockType.EIM_PARTY, true);
|
||||
private final Lock sL = new MonitoredReentrantLock(MonitoredLockType.EIM_SCRIPT, true);
|
||||
private final MonitoredReentrantLock pL = new MonitoredReentrantLock(MonitoredLockType.EIM_PARTY, true);
|
||||
private final MonitoredReentrantLock sL = new MonitoredReentrantLock(MonitoredLockType.EIM_SCRIPT, true);
|
||||
|
||||
private ScheduledFuture<?> event_schedule = null;
|
||||
private boolean disposed = false;
|
||||
@@ -638,13 +637,6 @@ public class EventInstanceManager {
|
||||
return (kc == null) ? 0 : kc;
|
||||
}
|
||||
|
||||
public void cancelSchedule() {
|
||||
if(event_schedule != null) {
|
||||
event_schedule.cancel(false);
|
||||
event_schedule = null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void dispose() {
|
||||
if(disposed) return;
|
||||
|
||||
@@ -673,9 +665,14 @@ public class EventInstanceManager {
|
||||
wL.unlock();
|
||||
}
|
||||
|
||||
cancelSchedule();
|
||||
if(event_schedule != null) {
|
||||
event_schedule.cancel(false);
|
||||
event_schedule = null;
|
||||
}
|
||||
|
||||
killCount.clear();
|
||||
mapIds.clear();
|
||||
props.clear();
|
||||
|
||||
disposeExpedition();
|
||||
|
||||
@@ -686,14 +683,24 @@ public class EventInstanceManager {
|
||||
} finally {
|
||||
sL.unlock();
|
||||
}
|
||||
|
||||
disposeLocks();
|
||||
}
|
||||
|
||||
private void disposeLocks() {
|
||||
pL.dispose();
|
||||
sL.dispose();
|
||||
}
|
||||
|
||||
public MapleMapFactory getMapFactory() {
|
||||
return mapFactory;
|
||||
}
|
||||
|
||||
public void schedule(final String methodName, long delay) {
|
||||
TimerManager.getInstance().schedule(new Runnable() {
|
||||
List<MapleCharacter> chrList = this.getPlayerList();
|
||||
int mapid = !chrList.isEmpty() ? chrList.get(0).getMapId() : 0;
|
||||
|
||||
Runnable r = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
@@ -708,7 +715,9 @@ public class EventInstanceManager {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, delay);
|
||||
};
|
||||
|
||||
getEm().getChannelServer().registerEventAction(mapid, r, delay);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
||||
@@ -26,7 +26,6 @@ import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -42,7 +41,7 @@ import net.server.channel.Channel;
|
||||
import net.server.guild.MapleGuild;
|
||||
import net.server.world.MapleParty;
|
||||
import net.server.world.MaplePartyCharacter;
|
||||
import server.TimerManager;
|
||||
import scripting.event.worker.EventScriptScheduler;
|
||||
import server.expeditions.MapleExpedition;
|
||||
import server.maps.MapleMap;
|
||||
import server.life.MapleMonster;
|
||||
@@ -53,7 +52,6 @@ import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import net.server.audit.locks.MonitoredLockType;
|
||||
import net.server.audit.locks.MonitoredReentrantLock;
|
||||
|
||||
@@ -67,6 +65,7 @@ public class EventManager {
|
||||
private Channel cserv;
|
||||
private World wserv;
|
||||
private Server server;
|
||||
private EventScriptScheduler ess = new EventScriptScheduler();
|
||||
private Map<String, EventInstanceManager> instances = new HashMap<String, EventInstanceManager>();
|
||||
private Map<String, Integer> instanceLocks = new HashMap<String, Integer>();
|
||||
private final Queue<Integer> queuedGuilds = new LinkedList<>();
|
||||
@@ -76,8 +75,8 @@ public class EventManager {
|
||||
private Integer readyId = 0;
|
||||
private Properties props = new Properties();
|
||||
private String name;
|
||||
private Lock lobbyLock = new MonitoredReentrantLock(MonitoredLockType.EM_LOBBY);
|
||||
private Lock queueLock = new MonitoredReentrantLock(MonitoredLockType.EM_QUEUE);
|
||||
private MonitoredReentrantLock lobbyLock = new MonitoredReentrantLock(MonitoredLockType.EM_LOBBY);
|
||||
private MonitoredReentrantLock queueLock = new MonitoredReentrantLock(MonitoredLockType.EM_QUEUE);
|
||||
|
||||
private static final int maxLobbys = 8; // an event manager holds up to this amount of concurrent lobbys
|
||||
|
||||
@@ -92,12 +91,47 @@ public class EventManager {
|
||||
for(int i = 0; i < maxLobbys; i++) this.openedLobbys.add(false);
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
public void cancel() { // make sure to only call this when there are NO PLAYERS ONLINE to mess around with the event manager!
|
||||
ess.dispose();
|
||||
|
||||
try {
|
||||
iv.invokeFunction("cancelSchedule", (Object) null);
|
||||
} catch (ScriptException | NoSuchMethodException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
synchronized(instances) {
|
||||
for(EventInstanceManager eim : instances.values()) {
|
||||
eim.dispose();
|
||||
}
|
||||
instances.clear();
|
||||
}
|
||||
|
||||
List<EventInstanceManager> readyEims;
|
||||
queueLock.lock();
|
||||
try {
|
||||
readyEims = new ArrayList<>(readyInstances);
|
||||
readyInstances.clear();
|
||||
} finally {
|
||||
queueLock.unlock();
|
||||
}
|
||||
|
||||
for(EventInstanceManager eim : readyEims) {
|
||||
eim.dispose();
|
||||
}
|
||||
|
||||
props.clear();
|
||||
cserv = null;
|
||||
wserv = null;
|
||||
server = null;
|
||||
iv = null;
|
||||
|
||||
disposeLocks();
|
||||
}
|
||||
|
||||
private void disposeLocks() {
|
||||
lobbyLock.dispose();
|
||||
queueLock.dispose();
|
||||
}
|
||||
|
||||
private static List<Integer> convertToIntegerArray(List<Double> list) {
|
||||
@@ -123,12 +157,12 @@ public class EventManager {
|
||||
}
|
||||
}
|
||||
|
||||
public ScheduledFuture<?> schedule(String methodName, long delay) {
|
||||
public EventScheduledFuture schedule(String methodName, long delay) {
|
||||
return schedule(methodName, null, delay);
|
||||
}
|
||||
|
||||
public ScheduledFuture<?> schedule(final String methodName, final EventInstanceManager eim, long delay) {
|
||||
return TimerManager.getInstance().schedule(new Runnable() {
|
||||
public EventScheduledFuture schedule(final String methodName, final EventInstanceManager eim, long delay) {
|
||||
Runnable r = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
@@ -137,11 +171,16 @@ public class EventManager {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}, delay);
|
||||
};
|
||||
|
||||
ess.registerEntry(r, delay);
|
||||
|
||||
// hate to do that, but those schedules can still be cancelled, so well... Let GC do it's job
|
||||
return new EventScheduledFuture(r, ess);
|
||||
}
|
||||
|
||||
public ScheduledFuture<?> scheduleAtTimestamp(final String methodName, long timestamp) {
|
||||
return TimerManager.getInstance().scheduleAtTimestamp(new Runnable() {
|
||||
public EventScheduledFuture scheduleAtTimestamp(final String methodName, long timestamp) {
|
||||
Runnable r = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
@@ -150,7 +189,10 @@ public class EventManager {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}, timestamp);
|
||||
};
|
||||
|
||||
ess.registerEntry(r, timestamp - server.getCurrentTime());
|
||||
return new EventScheduledFuture(r, ess);
|
||||
}
|
||||
|
||||
public World getWorldServer() {
|
||||
@@ -187,7 +229,7 @@ public class EventManager {
|
||||
}
|
||||
|
||||
public void disposeInstance(final String name) {
|
||||
TimerManager.getInstance().schedule(new Runnable() {
|
||||
ess.registerEntry(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
freeLobbyInstance(name);
|
||||
|
||||
40
src/scripting/event/EventScheduledFuture.java
Normal file
40
src/scripting/event/EventScheduledFuture.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
This file is part of the HeavenMS MapleStory Server
|
||||
Copyleft 2016 - 2018 RonanLana
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation version 3 as published by
|
||||
the Free Software Foundation. You may not use, modify or distribute
|
||||
this program under any other version of the GNU Affero General Public
|
||||
License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package scripting.event;
|
||||
|
||||
import scripting.event.worker.EventScriptScheduler;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Ronan
|
||||
*/
|
||||
public class EventScheduledFuture {
|
||||
Runnable r;
|
||||
EventScriptScheduler ess;
|
||||
|
||||
public EventScheduledFuture(Runnable r, EventScriptScheduler ess) {
|
||||
this.r = r;
|
||||
this.ess = ess;
|
||||
}
|
||||
|
||||
public void cancel(boolean dummy) { // will always implement "non-interrupt if running" regardless of boolean value
|
||||
ess.cancelEntry(r);
|
||||
}
|
||||
}
|
||||
169
src/scripting/event/worker/EventScriptScheduler.java
Normal file
169
src/scripting/event/worker/EventScriptScheduler.java
Normal file
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
This file is part of the HeavenMS MapleStory Server
|
||||
Copyleft 2016 - 2018 RonanLana
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation version 3 as published by
|
||||
the Free Software Foundation. You may not use, modify or distribute
|
||||
this program under any other version of the GNU Affero General Public
|
||||
License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package scripting.event.worker;
|
||||
|
||||
import constants.ServerConstants;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import server.TimerManager;
|
||||
import net.server.Server;
|
||||
import net.server.audit.locks.MonitoredLockType;
|
||||
import net.server.audit.locks.MonitoredReentrantLock;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Ronan
|
||||
*/
|
||||
public class EventScriptScheduler {
|
||||
private boolean disposed = false;
|
||||
private int idleProcs = 0;
|
||||
private Map<Runnable, Long> registeredEntries = new HashMap<>();
|
||||
|
||||
private ScheduledFuture<?> schedulerTask = null;
|
||||
private MonitoredReentrantLock schedulerLock;
|
||||
private Runnable monitorTask = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
runBaseSchedule();
|
||||
}
|
||||
};
|
||||
|
||||
public EventScriptScheduler() {
|
||||
schedulerLock = new MonitoredReentrantLock(MonitoredLockType.EM_SCHDL, true);
|
||||
}
|
||||
|
||||
private void runBaseSchedule() {
|
||||
List<Runnable> toRemove;
|
||||
Map<Runnable, Long> registeredEntriesCopy;
|
||||
|
||||
schedulerLock.lock();
|
||||
try {
|
||||
if(registeredEntries.isEmpty()) {
|
||||
idleProcs++;
|
||||
|
||||
if(idleProcs >= ServerConstants.MOB_STATUS_MONITOR_LIFE) {
|
||||
if(schedulerTask != null) {
|
||||
schedulerTask.cancel(false);
|
||||
schedulerTask = null;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
idleProcs = 0;
|
||||
registeredEntriesCopy = new HashMap<>(registeredEntries);
|
||||
} finally {
|
||||
schedulerLock.unlock();
|
||||
}
|
||||
|
||||
long timeNow = Server.getInstance().getCurrentTime();
|
||||
toRemove = new LinkedList<>();
|
||||
for(Entry<Runnable, Long> rmd : registeredEntriesCopy.entrySet()) {
|
||||
if(rmd.getValue() < timeNow) {
|
||||
Runnable r = rmd.getKey();
|
||||
|
||||
r.run(); // runs the scheduled action
|
||||
toRemove.add(r);
|
||||
}
|
||||
}
|
||||
|
||||
if(!toRemove.isEmpty()) {
|
||||
schedulerLock.lock();
|
||||
try {
|
||||
for(Runnable r : toRemove) {
|
||||
registeredEntries.remove(r);
|
||||
}
|
||||
} finally {
|
||||
schedulerLock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void registerEntry(final Runnable scheduledAction, final long duration) {
|
||||
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
schedulerLock.lock();
|
||||
try {
|
||||
idleProcs = 0;
|
||||
if(schedulerTask == null) {
|
||||
if(disposed) return;
|
||||
|
||||
schedulerTask = TimerManager.getInstance().register(monitorTask, ServerConstants.MOB_STATUS_MONITOR_PROC, ServerConstants.MOB_STATUS_MONITOR_PROC);
|
||||
}
|
||||
|
||||
registeredEntries.put(scheduledAction, Server.getInstance().getCurrentTime() + duration);
|
||||
} finally {
|
||||
schedulerLock.unlock();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
t.start();
|
||||
}
|
||||
|
||||
public void cancelEntry(final Runnable scheduledAction) {
|
||||
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
schedulerLock.lock();
|
||||
try {
|
||||
registeredEntries.remove(scheduledAction);
|
||||
} finally {
|
||||
schedulerLock.unlock();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
t.start();
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
schedulerLock.lock();
|
||||
try {
|
||||
if(schedulerTask != null) {
|
||||
schedulerTask.cancel(false);
|
||||
schedulerTask = null;
|
||||
}
|
||||
|
||||
registeredEntries.clear();
|
||||
disposed = true;
|
||||
} finally {
|
||||
schedulerLock.unlock();
|
||||
}
|
||||
|
||||
schedulerLock.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
t.start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user