Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cstore_doMaintenance Availability group check failing #19

Open
suggy1982 opened this issue Dec 21, 2017 · 2 comments
Open

cstore_doMaintenance Availability group check failing #19

suggy1982 opened this issue Dec 21, 2017 · 2 comments

Comments

@suggy1982
Copy link

Within the stored procedure there is a check to see if a database is part of an Always On availability group so that it doesn't execute on a read only secondary replica.

	IF exists (select *
					from sys.databases databases
					  INNER JOIN sys.availability_databases_cluster adc 
						ON databases.group_database_id = adc.group_database_id
					  INNER JOIN sys.availability_groups ag 
						ON adc.group_id = ag.group_id
					  WHERE databases.name = DB_NAME() )
		
	begin
		declare @replicaStatus int;
		select @replicaStatus = sys.fn_hadr_is_primary_replica ( DB_NAME() );
		SELECT @replicaStatus
		
		if @replicaStatus <> 1 
		begin 
			BEGIN
				SELECT 'Secondary Replica'
			end
		end 
		return;
	end

This check is failing and stopping the rest of the procedure from running, even when the database is on a primary replica. I believe this is due to the IS NOT NULL check on the @replicaStatus, if this is removed it should then work.

	IF exists (select *
					from sys.databases databases
					  INNER JOIN sys.availability_databases_cluster adc 
						ON databases.group_database_id = adc.group_database_id
					  INNER JOIN sys.availability_groups ag 
						ON adc.group_id = ag.group_id
					  WHERE databases.name = DB_NAME() )
		
	begin
		declare @replicaStatus int;
		select @replicaStatus = sys.fn_hadr_is_primary_replica ( DB_NAME() );
		SELECT @replicaStatus
		
		if @replicaStatus <> 1 
		begin 
			BEGIN
				SELECT 'Secondary Replica'
			end
		end 
		return;
	end
@suggy1982
Copy link
Author

Also the return needs to be move into the begin...end block

	IF exists (select *
					from sys.databases databases
					  INNER JOIN sys.availability_databases_cluster adc 
						ON databases.group_database_id = adc.group_database_id
					  INNER JOIN sys.availability_groups ag 
						ON adc.group_id = ag.group_id
					  WHERE databases.name = DB_NAME() )
		
	begin
		declare @replicaStatus int;
		select @replicaStatus = sys.fn_hadr_is_primary_replica ( DB_NAME() );
		SELECT @replicaStatus
		
		if @replicaStatus <> 1 
		begin 
			BEGIN
				SELECT 'Secondary Replica'
			END
            return;
		end 
		
	END
    SELECT 'primary'

@NikoNeugebauer
Copy link
Owner

Thank you for reporting, I will investigate this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants