Allow to configure core.packedGitOpenFiles

Allow to configure the maximum number of pack files to have open at once.

Feature: Issue 13725
Change-Id: I6d7a7e573778eb636487d9677b2c6b33b7734c5d
diff --git a/Configuration.md b/Configuration.md
index 2985e5b..194ef3c 100644
--- a/Configuration.md
+++ b/Configuration.md
@@ -43,6 +43,9 @@
 * `JGIT_CACHE_SIZE`: Maximum number of bytes to load and cache in memory from pack files.
   See [Gerrit documentation](https://gerrit-review.googlesource.com/Documentation/config-gerrit.html#core.packedGitLimit)
   for more details. `3g` by default.
+* `JGIT_OPEN_FILES`: Maximum number of pack files to have open at once.
+  See [Gerrit documentation](https://gerrit-review.googlesource.com/Documentation/config-gerrit.html#core.packedGitOpenFiles)
+  for more details. `128` by default.
 * `GERRIT_CONTAINER_FDS_SOFT_LIMIT`: The soft limit for file descriptors allowed in the Gerrit container.
 `1024` by default.
 * `GERRIT_CONTAINER_FDS_HARD_LIMIT`: The hard limit for file descriptors allowed in the Gerrit container
diff --git a/Makefile.common b/Makefile.common
index 8f52152..11b76e8 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -75,4 +75,9 @@
 endif
 ifdef GERRIT_CONTAINER_FDS_HARD_LIMIT
 		$(eval GERRIT_ULIMITS := $(GERRIT_ULIMITS) ParameterKey=FileDescriptorsHardLimit,ParameterValue=$(GERRIT_CONTAINER_FDS_HARD_LIMIT))
+endif
+
+set-optional-jgit-conf:
+ifdef JGIT_OPEN_FILES
+		$(eval JGIT_OPTIONAL_PARAMS := ParameterKey=JgitOpenFiles,ParameterValue=$(JGIT_OPEN_FILES))
 endif
\ No newline at end of file
diff --git a/dual-master/Makefile b/dual-master/Makefile
index c00cc72..ca00f03 100644
--- a/dual-master/Makefile
+++ b/dual-master/Makefile
@@ -86,7 +86,7 @@
 		ParameterKey=SubnetIdProp,ParameterValue=$(SUBNET_ID) \
 		$(CLUSTER_OPTIONAL_PARAMS)
 
-service-master-1: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-optional-params-multisite set-ldap-account-pattern set-optional-gerrit-ulimits
+service-master-1: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-optional-params-multisite set-ldap-account-pattern set-optional-gerrit-ulimits set-optional-jgit-conf
 ifdef GERRIT_MASTER1_INSTANCE_ID
 		$(eval MASTER1_SERVICE_OPTIONAL_PARAMS := $(MASTER1_SERVICE_OPTIONAL_PARAMS) ParameterKey=InstanceId,ParameterValue=$(GERRIT_MASTER1_INSTANCE_ID))
 endif
@@ -131,6 +131,7 @@
 		ParameterKey=GerritCPU,ParameterValue=$(GERRIT_CPU) \
 		ParameterKey=GerritHeapLimit,ParameterValue=$(GERRIT_HEAP_LIMIT) \
 		ParameterKey=JgitCacheSize,ParameterValue=$(JGIT_CACHE_SIZE) \
+		$(JGIT_OPTIONAL_PARAMS) \
 		$(MULTISITE_OPTIONAL_PARAMS) \
 		$(REMOTE_OPTIONAL_PARAMS) \
 		$(MASTER1_SERVICE_OPTIONAL_PARAMS) \
@@ -139,7 +140,7 @@
 		$(SMTP_OPTIONAL_PARAMS) \
 		$(GERRIT_ULIMITS)
 
-service-master-2: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-optional-params-multisite set-ldap-account-pattern set-optional-gerrit-ulimits
+service-master-2: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-optional-params-multisite set-ldap-account-pattern set-optional-gerrit-ulimits set-optional-jgit-conf
 ifdef GERRIT_MASTER2_INSTANCE_ID
 		$(eval MASTER2_SERVICE_OPTIONAL_PARAMS := $(MASTER2_SERVICE_OPTIONAL_PARAMS) ParameterKey=InstanceId,ParameterValue=$(GERRIT_MASTER2_INSTANCE_ID))
 endif
@@ -185,6 +186,7 @@
 		ParameterKey=GerritCPU,ParameterValue=$(GERRIT_CPU) \
 		ParameterKey=GerritHeapLimit,ParameterValue=$(GERRIT_HEAP_LIMIT) \
 		ParameterKey=JgitCacheSize,ParameterValue=$(JGIT_CACHE_SIZE) \
+		$(JGIT_OPTIONAL_PARAMS) \
 		$(MULTISITE_OPTIONAL_PARAMS) \
 		$(REMOTE_OPTIONAL_PARAMS) \
 		$(MASTER2_SERVICE_OPTIONAL_PARAMS) \
@@ -209,7 +211,7 @@
 		ParameterKey=HostedZoneName,ParameterValue=$(HOSTED_ZONE_NAME) \
 		ParameterKey=GitReplicationSubdomain,ParameterValue=$(GIT_REPLICATION_SUBDOMAIN)
 
-service-slave: set-optional-params-metrics-cloudwatch set-ldap-account-pattern set-optional-gerrit-ulimits
+service-slave: set-optional-params-metrics-cloudwatch set-ldap-account-pattern set-optional-gerrit-ulimits set-optional-jgit-conf
 ifdef GERRIT_SLAVE_INSTANCE_ID
 		$(eval SLAVE_SERVICE_OPTIONAL_PARAMS := $(SLAVE_SERVICE_OPTIONAL_PARAMS) ParameterKey=InstanceId,ParameterValue=$(GERRIT_SLAVE_INSTANCE_ID))
 endif
@@ -239,6 +241,7 @@
 		ParameterKey=GerritCPU,ParameterValue=$(GERRIT_CPU) \
 		ParameterKey=GerritHeapLimit,ParameterValue=$(GERRIT_HEAP_LIMIT) \
 		ParameterKey=JgitCacheSize,ParameterValue=$(JGIT_CACHE_SIZE) \
+		$(JGIT_OPTIONAL_PARAMS) \
 		$(LDAP_ACCOUNT_PATTERN_PARAM) \
 		$(SLAVE_SERVICE_OPTIONAL_PARAMS) \
 		$(METRICS_CW_OPTIONAL_PARAMS) \
diff --git a/dual-master/cf-service-master.yml b/dual-master/cf-service-master.yml
index c2c8d85..6d8f49b 100644
--- a/dual-master/cf-service-master.yml
+++ b/dual-master/cf-service-master.yml
@@ -86,6 +86,10 @@
       Description: JGit cache size
       Type: String
       Default: 3g
+  JgitOpenFiles:
+      Description: Maximum number of pack files to have open at once
+      Type: Number
+      Default: 128
   LDAPServer:
       Description: LDAP server URL
       Type: String
@@ -272,6 +276,8 @@
                       Value: !Ref GerritHeapLimit
                     - Name: JGIT_CACHE_SIZE
                       Value: !Ref JgitCacheSize
+                    - Name: JGIT_OPEN_FILES
+                      Value: !Ref JgitOpenFiles
                     - Name: LDAP_SERVER
                       Value: !Ref LDAPServer
                     - Name: LDAP_USERNAME
diff --git a/dual-master/cf-service-slave.yml b/dual-master/cf-service-slave.yml
index 5c5acb3..41df598 100644
--- a/dual-master/cf-service-slave.yml
+++ b/dual-master/cf-service-slave.yml
@@ -134,6 +134,10 @@
       Description: JGit cache size
       Type: String
       Default: 3g
+  JgitOpenFiles:
+      Description: Maximum number of pack files to have open at once
+      Type: Number
+      Default: 128
   LDAPServer:
       Description: LDAP server URL
       Type: String
@@ -243,6 +247,8 @@
                       Value: !Ref GerritHeapLimit
                     - Name: JGIT_CACHE_SIZE
                       Value: !Ref JgitCacheSize
+                    - Name: JGIT_OPEN_FILES
+                      Value: !Ref JgitOpenFiles
                     - Name: LDAP_SERVER
                       Value: !Ref LDAPServer
                     - Name: LDAP_USERNAME
diff --git a/dual-master/setup.env.template b/dual-master/setup.env.template
index d9e6377..ce18bb5 100644
--- a/dual-master/setup.env.template
+++ b/dual-master/setup.env.template
@@ -32,6 +32,7 @@
 GERRIT_CPU=1024
 GERRIT_HEAP_LIMIT=6g
 JGIT_CACHE_SIZE=3g
+JGIT_OPEN_FILES=128
 
 FILESYSTEM_THROUGHPUT_MODE=bursting
 
diff --git a/gerrit/etc/gerrit.config.template b/gerrit/etc/gerrit.config.template
index e8392ac..a18b21b 100644
--- a/gerrit/etc/gerrit.config.template
+++ b/gerrit/etc/gerrit.config.template
@@ -11,6 +11,7 @@
 {% endif %}
 [core]
 	packedGitLimit = {{ JGIT_CACHE_SIZE }}
+	packedGitOpenFiles = {{ JGIT_OPEN_FILES }}
 [database]
 	type = h2
 	database = db/ReviewDB
diff --git a/gerrit/setup_gerrit.py b/gerrit/setup_gerrit.py
index 7db592a..f549b60 100755
--- a/gerrit/setup_gerrit.py
+++ b/gerrit/setup_gerrit.py
@@ -159,6 +159,7 @@
         'SMTP_SSL_VERIFY': os.getenv('SMTP_SSL_VERIFY'),
         'GERRIT_HEAP_LIMIT': os.getenv('GERRIT_HEAP_LIMIT'),
         'JGIT_CACHE_SIZE': os.getenv('JGIT_CACHE_SIZE'),
+        'JGIT_OPEN_FILES': os.getenv('JGIT_OPEN_FILES'),
         'GERRIT_INSTANCE_ID': os.getenv('GERRIT_INSTANCE_ID'),
         'METRICS_CLOUDWATCH_ENABLED': os.getenv('METRICS_CLOUDWATCH_ENABLED'),
         'METRICS_CLOUDWATCH_NAMESPACE': os.getenv('METRICS_CLOUDWATCH_NAMESPACE'),
diff --git a/master-slave/Makefile b/master-slave/Makefile
index 2dfa92e..89f4526 100644
--- a/master-slave/Makefile
+++ b/master-slave/Makefile
@@ -53,7 +53,7 @@
 		ParameterKey=SubnetIdProp,ParameterValue=$(SUBNET_ID) \
 		$(CLUSTER_OPTIONAL_PARAMS)
 
-service-master: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-ldap-account-pattern set-optional-gerrit-ulimits
+service-master: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-ldap-account-pattern set-optional-gerrit-ulimits set-optional-jgit-conf
 ifdef LOAD_BALANCER_SCHEME
 		$(eval MASTER_SERVICE_OPTIONAL_PARAMS := $(MASTER_SERVICE_OPTIONAL_PARAMS) ParameterKey=LoadBalancerScheme,ParameterValue=$(LOAD_BALANCER_SCHEME))
 endif
@@ -88,6 +88,7 @@
 		ParameterKey=GerritCPU,ParameterValue=$(GERRIT_CPU) \
 		ParameterKey=GerritHeapLimit,ParameterValue=$(GERRIT_HEAP_LIMIT) \
 		ParameterKey=JgitCacheSize,ParameterValue=$(JGIT_CACHE_SIZE) \
+		$(JGIT_OPTIONAL_PARAMS) \
 		$(LDAP_ACCOUNT_PATTERN_PARAM) \
 		$(MASTER_SERVICE_OPTIONAL_PARAMS) \
 		$(METRICS_CW_OPTIONAL_PARAMS) \
@@ -95,7 +96,7 @@
 		$(GERRIT_ULIMITS)
 
 
-service-slave: set-optional-params-metrics-cloudwatch set-ldap-account-pattern set-optional-gerrit-ulimits
+service-slave: set-optional-params-metrics-cloudwatch set-ldap-account-pattern set-optional-gerrit-ulimits set-optional-jgit-conf
 ifdef LOAD_BALANCER_SCHEME
 		$(eval SLAVE_SERVICE_OPTIONAL_PARAMS := $(SLAVE_SERVICE_OPTIONAL_PARAMS) ParameterKey=LoadBalancerScheme,ParameterValue=$(LOAD_BALANCER_SCHEME))
 endif
@@ -125,6 +126,7 @@
 		ParameterKey=GerritCPU,ParameterValue=$(GERRIT_CPU) \
 		ParameterKey=GerritHeapLimit,ParameterValue=$(GERRIT_HEAP_LIMIT) \
 		ParameterKey=JgitCacheSize,ParameterValue=$(JGIT_CACHE_SIZE) \
+		$(JGIT_OPTIONAL_PARAMS) \
 		$(LDAP_ACCOUNT_PATTERN_PARAM) \
 		$(SLAVE_SERVICE_OPTIONAL_PARAMS) \
 		$(METRICS_CW_OPTIONAL_PARAMS) \
diff --git a/master-slave/cf-service-master.yml b/master-slave/cf-service-master.yml
index 86aa525..3c1f39b 100644
--- a/master-slave/cf-service-master.yml
+++ b/master-slave/cf-service-master.yml
@@ -122,6 +122,10 @@
       Description: JGit cache size
       Type: String
       Default: 3g
+  JgitOpenFiles:
+      Description: Maximum number of pack files to have open at once
+      Type: Number
+      Default: 128
   LDAPServer:
       Description: LDAP server URL
       Type: String
@@ -243,6 +247,8 @@
                       Value: !Ref GerritHeapLimit
                     - Name: JGIT_CACHE_SIZE
                       Value: !Ref JgitCacheSize
+                    - Name: JGIT_OPEN_FILES
+                      Value: !Ref JgitOpenFiles
                     - Name: LDAP_SERVER
                       Value: !Ref LDAPServer
                     - Name: LDAP_USERNAME
diff --git a/master-slave/cf-service-slave.yml b/master-slave/cf-service-slave.yml
index 0540fe9..80014f6 100644
--- a/master-slave/cf-service-slave.yml
+++ b/master-slave/cf-service-slave.yml
@@ -134,6 +134,10 @@
       Description: JGit cache size
       Type: String
       Default: 3g
+  JgitOpenFiles:
+      Description: Maximum number of pack files to have open at once
+      Type: Number
+      Default: 128
   LDAPServer:
       Description: LDAP server URL
       Type: String
@@ -240,6 +244,8 @@
                       Value: !Ref GerritHeapLimit
                     - Name: JGIT_CACHE_SIZE
                       Value: !Ref JgitCacheSize
+                    - Name: JGIT_OPEN_FILES
+                      Value: !Ref JgitOpenFiles
                     - Name: LDAP_SERVER
                       Value: !Ref LDAPServer
                     - Name: LDAP_USERNAME
diff --git a/master-slave/setup.env.template b/master-slave/setup.env.template
index 0c519c4..d87bbaa 100644
--- a/master-slave/setup.env.template
+++ b/master-slave/setup.env.template
@@ -19,6 +19,7 @@
 GERRIT_CPU=1024
 GERRIT_HEAP_LIMIT=6g
 JGIT_CACHE_SIZE=3g
+JGIT_OPEN_FILES=128
 
 GERRIT_MASTER_INSTANCE_ID=gerrit-master-slave-MASTER
 GERRIT_SLAVE_INSTANCE_ID=gerrit-master-slave-SLAVE
diff --git a/single-master/Makefile b/single-master/Makefile
index 9076898..dda38cc 100644
--- a/single-master/Makefile
+++ b/single-master/Makefile
@@ -52,7 +52,7 @@
 		ParameterKey=SubnetIdProp,ParameterValue=$(SUBNET_ID) \
 		$(CLUSTER_OPTIONAL_PARAMS)
 
-service: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-ldap-account-pattern set-optional-gerrit-ulimits
+service: set-optional-params-metrics-cloudwatch set-optional-params-smtp set-ldap-account-pattern set-optional-gerrit-ulimits set-optional-jgit-conf
 ifdef LOAD_BALANCER_SCHEME
 		$(eval SERVICE_OPTIONAL_PARAMS := $(SERVICE_OPTIONAL_PARAMS) ParameterKey=LoadBalancerScheme,ParameterValue=$(LOAD_BALANCER_SCHEME))
 endif
@@ -85,6 +85,7 @@
 		ParameterKey=GerritCPU,ParameterValue=$(GERRIT_CPU) \
 		ParameterKey=GerritHeapLimit,ParameterValue=$(GERRIT_HEAP_LIMIT) \
 		ParameterKey=JgitCacheSize,ParameterValue=$(JGIT_CACHE_SIZE) \
+		$(JGIT_OPTIONAL_PARAMS) \
 		$(LDAP_ACCOUNT_PATTERN_PARAM) \
 		$(SERVICE_OPTIONAL_PARAMS) \
 		$(METRICS_CW_OPTIONAL_PARAMS) \
diff --git a/single-master/cf-service.yml b/single-master/cf-service.yml
index cc6f12a..9cbba11 100644
--- a/single-master/cf-service.yml
+++ b/single-master/cf-service.yml
@@ -104,6 +104,10 @@
       Description: JGit cache size
       Type: String
       Default: 3g
+  JgitOpenFiles:
+      Description: Maximum number of pack files to have open at once
+      Type: Number
+      Default: 128
   LDAPServer:
       Description: LDAP server URL
       Type: String
@@ -227,6 +231,8 @@
                       Value: !Ref GerritHeapLimit
                     - Name: JGIT_CACHE_SIZE
                       Value: !Ref JgitCacheSize
+                    - Name: JGIT_OPEN_FILES
+                      Value: !Ref JgitOpenFiles
                     - Name: LDAP_SERVER
                       Value: !Ref LDAPServer
                     - Name: LDAP_USERNAME
diff --git a/single-master/setup.env.template b/single-master/setup.env.template
index a9609d0..0947477 100644
--- a/single-master/setup.env.template
+++ b/single-master/setup.env.template
@@ -9,6 +9,7 @@
 GERRIT_CPU=1024
 GERRIT_HEAP_LIMIT=6g
 JGIT_CACHE_SIZE=3g
+JGIT_OPEN_FILES=128
 
 GERRIT_INSTANCE_ID=gerrit-single-master