Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
K
key_value-server
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
amir.yosef
key_value-server
Commits
988c6bf3
Commit
988c6bf3
authored
Aug 13, 2024
by
amir.yosef
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Applying ServerBuilder correctly
parent
a16f4887
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
11 additions
and
31 deletions
+11
-31
Main.java
src/Main.java
+2
-2
Director.java
src/director/Director.java
+3
-1
Server.java
src/server/Server.java
+0
-10
ServerBuilder.java
src/server/ServerBuilder.java
+2
-10
StorageManager.java
src/storage/StorageManager.java
+4
-8
No files found.
src/Main.java
View file @
988c6bf3
import
builde
r.Director
;
import
directo
r.Director
;
import
build
er.ServerBuilder
;
import
serv
er.ServerBuilder
;
import
server.Server
;
import
server.Server
;
public
class
Main
{
public
class
Main
{
...
...
src/
builde
r/Director.java
→
src/
directo
r/Director.java
View file @
988c6bf3
package
builder
;
package
director
;
import
server.ServerBuilder
;
public
class
Director
{
public
class
Director
{
public
void
buildMaster
(
ServerBuilder
builder
)
{
public
void
buildMaster
(
ServerBuilder
builder
)
{
...
...
src/server/Server.java
View file @
988c6bf3
...
@@ -16,7 +16,6 @@ public class Server implements AutoCloseable {
...
@@ -16,7 +16,6 @@ public class Server implements AutoCloseable {
private
final
String
role
;
private
final
String
role
;
StorageManager
manager
=
new
StorageManager
();
StorageManager
manager
=
new
StorageManager
();
private
static
volatile
Server
instance
;
Server
(
int
port
,
String
role
)
{
Server
(
int
port
,
String
role
)
{
PORT
=
port
;
PORT
=
port
;
...
@@ -24,15 +23,6 @@ public class Server implements AutoCloseable {
...
@@ -24,15 +23,6 @@ public class Server implements AutoCloseable {
this
.
executor
=
Executors
.
newVirtualThreadPerTaskExecutor
();
this
.
executor
=
Executors
.
newVirtualThreadPerTaskExecutor
();
}
}
public
static
Server
getInstance
(
int
PORT
,
String
role
)
throws
IOException
{
if
(
role
.
equalsIgnoreCase
(
"master"
))
{
if
(
instance
==
null
)
{
instance
=
new
Server
(
PORT
,
role
);
}
}
return
instance
;
}
public
void
start
()
{
public
void
start
()
{
try
(
ServerSocket
serverSocket
=
new
ServerSocket
(
PORT
))
{
try
(
ServerSocket
serverSocket
=
new
ServerSocket
(
PORT
))
{
System
.
out
.
println
(
"Server started on port "
+
PORT
);
System
.
out
.
println
(
"Server started on port "
+
PORT
);
...
...
src/
build
er/ServerBuilder.java
→
src/
serv
er/ServerBuilder.java
View file @
988c6bf3
package
builder
;
package
server
;
import
server.Server
;
import
java.io.IOException
;
public
class
ServerBuilder
{
public
class
ServerBuilder
{
private
int
port
;
private
int
port
;
...
@@ -17,10 +13,6 @@ public class ServerBuilder {
...
@@ -17,10 +13,6 @@ public class ServerBuilder {
}
}
public
Server
build
()
{
public
Server
build
()
{
try
{
return
new
Server
(
port
,
role
);
return
Server
.
getInstance
(
port
,
role
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"Failed to build the Server"
);
}
}
}
}
}
src/storage/StorageManager.java
View file @
988c6bf3
package
storage
;
package
storage
;
import
java.util.Date
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
...
@@ -11,22 +12,17 @@ public class StorageManager {
...
@@ -11,22 +12,17 @@ public class StorageManager {
public
StorageManager
()
{
public
StorageManager
()
{
this
.
storage
=
Storage
.
getInstance
();
this
.
storage
=
Storage
.
getInstance
();
this
.
scheduler
=
Executors
.
newSingleThreadScheduledExecutor
();
this
.
scheduler
=
Executors
.
newSingleThreadScheduledExecutor
();
scheduler
.
scheduleAtFixedRate
(
this
::
performMaintenance
,
5
,
5
,
TimeUnit
.
MINUTES
);
scheduler
.
scheduleAtFixedRate
(
this
::
performMaintenance
,
2
,
2
,
TimeUnit
.
MINUTES
);
}
}
public
Storage
getStorage
()
{
return
storage
;
}
private
void
performMaintenance
()
{
private
void
performMaintenance
()
{
storage
.
runCachePolicy
();
storage
.
runCachePolicy
();
System
.
out
.
println
(
"Maintenance performed at: "
+
System
.
currentTimeMillis
(
));
System
.
out
.
println
(
"Maintenance performed at: "
+
Date
.
from
(
java
.
time
.
Clock
.
systemUTC
().
instant
()
));
}
}
public
void
shutdown
()
{
public
void
shutdown
()
{
scheduler
.
shutdown
();
scheduler
.
shutdown
();
try
{
try
{
if
(!
scheduler
.
awaitTermination
(
6
0
,
TimeUnit
.
SECONDS
))
{
if
(!
scheduler
.
awaitTermination
(
2
0
,
TimeUnit
.
SECONDS
))
{
scheduler
.
shutdownNow
();
scheduler
.
shutdownNow
();
}
}
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment